image.png

解题思路

递归来做

code

  1. public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
  2. if(t1==null)
  3. return t2;
  4. if(t2==null)
  5. return t1;
  6. TreeNode res = new TreeNode(t1.val+t2.val);
  7. res.left = mergeTrees(t1.left,t2.left);
  8. res.right = mergeTrees(t1.right,t2.right);
  9. return res;
  10. }

key

二叉树 递归