For Tree 3, its each node's val is the sum(TreeNode t1 + TreeNode t2) in the same location.
Way to go deeper
if (t1 !=NULL && t2 != NULL) {
t1 = t1 + t2;
}
else if(t1 == NULL && t2 == NULL){return}
else{if(t1==NULL){t1 = t2;}else if(t2==NULL){t1 = t1;}}
mergeTrees(node.left);
mergeTrees(node.right);
每次return不知道return到哪儿去了
if (t1 == null)
return t2;
if (t2 == null)
return t1;
// check
t1.val += t2.val;
// 赋值
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
// 继续调用,往下(recursive)
// 返回t1,给上一次调用他的mergeTrees((t1,t2).parent.left, (t1,t2).parent.left)
或者mergeTrees((t1,t2).parent.right, (t1,t2).parent.right)
SOLUTION
Approach #1 Using Recursion [Accepted]
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null) {return t2;}
if(t2 == null) {return t1;}
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
}