Easy java solution


  • 0
    S
        public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
            t1 = merge(t1, t2);
            return t1;
        }
        
        public TreeNode merge (TreeNode t1, TreeNode t2) {
            if (t1 == null) {
                t1 = t2;
            } else {
                if (t2 != null) {
                    t1.val += t2.val;
                    t1.left = merge(t1.left , t2.left);
                    t1.right = merge(t1.right, t2.right);
                }
            }
            
            return t1;
        }
    }

Log in to reply
 

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