Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, August 27, 2017

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;
     }

0 Comments:

Post a Comment

Stats