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);
// 繼續(xù)調(diào)用舶得,往下(recursive)
// 返回t1,給上一次調(diào)用他的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;
}
}