jw wrote:
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree
like in the preorder tecnique and add all the nodes to another root
is there a common way to do this?
I haven't tested this, but I think it would do the trick:
struct Node
{
Node *left;
Node *right;
Node *clone();
};
Node *Node::clone()
{
Node *node = new Node;
if(left)
node->left = left->clone();
if(right)
node->right = right->clone();
return node;
}
Given the root node, the entire tree will be cloned by:
Node *rootClone = root->clone();
DW