public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
// write code here
if(t1 == null){
return t2;
}
if(t2 == null){
return t1;
}
t1.val = t1.val + t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
对于二叉树的算法要把握两个关键点:
1、对于树的递归,可以先判断特殊情况,就是说为空的情况,二叉树为空的情况一般就是递归出口
2、把递归出口弄清楚之后,再处理细节问题