Clone a Binary Tree
Code:
public static void clone(Tree t,Tree n)
{
if(t==null)
return ;
if(t.left!=null)
n.left=new Tree(t.left.data);
n.random=t.random;
if(t.right!=null)
n.right=new Tree(t.right.data);
clone(t.left,n.left);
clone(t.right,n.right);
}
public static Tree cloneTree(Tree tree){
Tree n=new Tree(tree.data);
clone(tree,n);
return n;
}
public static void clone(Tree t,Tree n)
{
if(t==null)
return ;
if(t.left!=null)
n.left=new Tree(t.left.data);
n.random=t.random;
if(t.right!=null)
n.right=new Tree(t.right.data);
clone(t.left,n.left);
clone(t.right,n.right);
}
public static Tree cloneTree(Tree tree){
Tree n=new Tree(tree.data);
clone(tree,n);
return n;
}
0 Comments:
Post a Comment