avatar
k*r
1
Write a method: Node* Copy(Node* root)
that takes a pointer to a Node structure as a parameter. The
Node structure contains two pointers to other Node structures. The function
should return a complete copy of the passed-in data structure.
请问是这样做吗?
typedef struct node{
int value;
struct node *child1;
struct node *child2;
}Node;
Node* Copy(Node* root){
Node *n = new Node;
Copywrapper(n, root);
return n;
}
void Copywrapper(Node* n, Node* root){
if(!root) return;
n->v
avatar
s*e
2
there might be a circle in the tree structure which need to be considered.
so we might need to store the copied nodes use a hash or map
node * completecopy_help(node* root, std::map& copied){
if(!root)return null;
if(copied[root])return copied[root];
// not copied before
node * r=new node;
copied[root]=r;
r->left= completecopy_help(root->left,copied);
r->right=completecopy_help(root->right,copied);
return r;

}
node* copy(node* root){
std::map
avatar
k*r
3
3x. 树还会loop啊?

【在 s***e 的大作中提到】
: there might be a circle in the tree structure which need to be considered.
: so we might need to store the copied nodes use a hash or map
: node * completecopy_help(node* root, std::map& copied){
: if(!root)return null;
: if(copied[root])return copied[root];
: // not copied before
: node * r=new node;
: copied[root]=r;
: r->left= completecopy_help(root->left,copied);
: r->right=completecopy_help(root->right,copied);

avatar
k*k
4
I don't think so...

【在 k********r 的大作中提到】
: 3x. 树还会loop啊?
avatar
t*t
5
如果是树,当然不会
但是问题在于,原题中有说这是树吗?

【在 k********r 的大作中提到】
: 3x. 树还会loop啊?
avatar
c*x
6
The question is to test you to write a recursive funtion.
avatar
c*e
7
Node *copy(Node *root)
{
if (root == NULL) return NULL;
Node *newnode = new Node;
newnode->value = root->value;
newnode->child1=copy(root->child1);
newnode->child2=copy(root->child2);
return newnode;
}
avatar
c*x
8

very nice.

【在 c****e 的大作中提到】
: Node *copy(Node *root)
: {
: if (root == NULL) return NULL;
: Node *newnode = new Node;
: newnode->value = root->value;
: newnode->child1=copy(root->child1);
: newnode->child2=copy(root->child2);
: return newnode;
: }

avatar
d*d
9
DFS,BFS都可以.

function

【在 k********r 的大作中提到】
: Write a method: Node* Copy(Node* root)
: that takes a pointer to a Node structure as a parameter. The
: Node structure contains two pointers to other Node structures. The function
: should return a complete copy of the passed-in data structure.
: 请问是这样做吗?
: typedef struct node{
: int value;
: struct node *child1;
: struct node *child2;
: }Node;

avatar
w*i
10
原题中根本就没有提要copy的是什么data structure
原题:
Write a method that takes a pointer to a Node structure as a parameter. The
Node structure contains two pointers to other Node structures. The function
should return a complete copy of the passed-in data structure.
For example, the method signature could look like so:
Node* Copy(Node* root);
Note:
· Do not make any assumptions about the data structure – it could
be a tree, linked list, graph etc.
· Feel free to choose the language you are most comfortable wit
avatar
l*a
11

neat!
in case there are loops, maybe it is better to mark the visted nodes and
check before copy.

【在 c****e 的大作中提到】
: Node *copy(Node *root)
: {
: if (root == NULL) return NULL;
: Node *newnode = new Node;
: newnode->value = root->value;
: newnode->child1=copy(root->child1);
: newnode->child2=copy(root->child2);
: return newnode;
: }

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。