Java Solution 12ms, Beats 100%


  • 0
    L
        public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
            helper(t1, t2);
            return t1 == null ? t2 : t1;
        }
        
        public void helper(TreeNode t1, TreeNode t2){
            if(t2 == null) return;
            if(t1 != null){
                t1.val += t2.val;
                if(t1.left == null) t1.left = t2.left;
                else helper(t1.left, t2.left);
                if(t1.right == null) t1.right = t2.right;
                else helper(t1.right, t2.right);
            }
            return;
        }
    

  • 0
    L

    The general idea is to start with the first tree, and add branches if necessary.


Log in to reply
 

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