My Java Solution, beats 60% solution.


  • 0
    P
     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
            TreeNode t3=new TreeNode(0);
            t3=traverse(t1,t2);
            return t3;
        }
        
        public TreeNode traverse(TreeNode t1,TreeNode t2){
            TreeNode t3=new TreeNode(0);
            if(t1 ==null && t2==null){
                return null;
            }else if(t1!=null && t2==null){
                t3=t1;
            }else if(t1==null && t2!=null){
                t3=t2;
            }else if (t1!=null && t2!=null){
                t3.val=t1.val+ t2.val;
                t3.left = new TreeNode(0);
                t3.left = traverse(t1.left,t2.left);
                t3.right =new TreeNode(0);
                t3.right= traverse(t1.right,t2.right);
            }
            return t3;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.