using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param t1 TreeNode类 * @param t2 TreeNode类 * @return TreeNode类 */ public TreeNode mergeTrees (TreeNode t1, TreeNode t2) { if(t1 == null) return t2; if(t2 == null) return t1; MergeTrees2(t1, t2); return t1; } public void MergeTrees2(TreeNode root1, TreeNode root2){ root1.val = root1.val + root2.val; if(root1.left == null || root2.left == null) { if(root1.left == null) root1.left = root2.left; } else MergeTrees2(root1.left, root2.left); if(root1.right == null || root2.right == null) { if(root1.right == null) root1.right = root2.right; } else MergeTrees2(root1.right, root2.right); return; } }