简单直接的版本:把两个树合并到t1。若两个树都存在,直接把和放在t1中,然后再合并子树。合并子树之前进行一下t1的空节点判断即可
import java.util.*;
public class Solution {
public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
if(t1!=null&&t2!=null){
t1.val = t1.val+t2.val;
if(t1.left!=null&&t2.left!=null)
mergeTrees(t1.left,t2.left);
if(t1.right!=null&&t2.right!=null)
mergeTrees(t1.right,t2.right);
if(t1.left==null)
t1.left = t2.left;
if(t1.right==null)
t1.right = t2.right;
}
return t1;
}
}