一:题目
二:上码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {/**1.前序遍历2.递归终止条件:(只考虑单层循环)当我们遍历到某个二叉树的某个结点为空时,返回另一个二叉树的在该位置对应的结点3.递归体的话 我们是选择root1上进行填补*///单层递归逻辑public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if (root1 == null) return root2;if (root2 == null) return root1;root1.val += root2.val;root1.left = mergeTrees(root1.left,root2.left);root1.right = mergeTrees(root1.right,root2.right); return root1;}
}