LeetCode 617. Merge Two Binary Trees

You are given two binary trees root1 and root2.

Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.

Return the merged tree.

 

Note: The merging process must start from the root nodes of both trees.

Example 1:

Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output: [3,4,5,5,4,null,7]

Example 2:

Input: root1 = [1], root2 = [1,2]
Output: [2,2]

Constraints:

  • The number of nodes in both trees is in the range [0, 2000].
  • -104 <= Node.val <= 104


这题的merge的意思是,把两棵树相同位置的节点的val加起来,形成一棵新的树。

递归的方法还比较好想,但是脑子不行刚开始也没憋出来,瞄了一眼答案才想到。其实也是把这个问题拆分为子问题,对于t1 t2两个root,两个都空就return null,至少有一个就return那一个,如果两个都有就只需要把他们的val相加,他们的left和right继续递归就好了。刚开始被confuse的点在于,这个函数return的是一个TreeNode,其实直接替换掉原来的一个root就行了。然后又想岔了,忘了直接root.left和root.right,反倒在想怎么把当前的root接到上面的root……

Runtime: 0 ms, faster than 100.00% of Java online submissions for Merge Two Binary Trees.

Memory Usage: 39 MB, less than 15.39% of Java online submissions for Merge Two Binary Trees.

/**
 * 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 {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) {
            return null;
        }
        if (t1 == null) {
            return t2;
        }
        if (t2 == null) {
            return t1;
        }
        TreeNode node = new TreeNode(t1.val + t2.val);
        node.left = mergeTrees(t1.left, t2.left);
        node.right = mergeTrees(t1.right, t2.right);
        return node;
    }
}

迭代,嗯,是我太蠢了,可能是没想明白。其实知道可以用两个queue/stack来分别存两棵树的内容,但是在while里的判断条件的地方一直没想明白。其实还是很直观的。如果t1和t2任何一个为null,其实都不需要做任何事,因为直接take另一个。如果两个都不为null,那就两个val相加,然后处理它们的左右子树。如果t1的左子树不存在,那就直接给t2的左子树放到t1的左子树上(管它是不是null呢),就完事儿了;如果t1的左子树存在,那就把t1的左子树和t2的左子树都放进queue/stack里(管t2的左子树是不是null呢,是null的话前面已经处理了)。对右子树也是一样的处理方式。嗯,就是没想到有这种分情况讨论的方式,还是我太愚蠢了。

/**
 * 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 {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) {
            return null;
        }
        if (t1 != null && t2 == null) {
            return t1;
        }
        if (t1 == null && t2 != null) {
            return t2;
        }
        Queue<TreeNode> q1 = new LinkedList<>();
        Queue<TreeNode> q2 = new LinkedList<>();
        q1.add(t1);
        q2.add(t2);
        while (!q1.isEmpty() && !q2.isEmpty()) {
            TreeNode node1 = q1.remove();
            TreeNode node2 = q2.remove();
            if (node1 == null || node2 == null) {
                continue;
            }
            node1.val += node2.val;
            if (node1.left == null) {
                node1.left = node2.left;
            } else {
                q1.add(node1.left);
                q2.add(node2.left);
            }
            if (node1.right == null) {
                node1.right = node2.right;
            } else {
                q1.add(node1.right);
                q2.add(node2.right);
            }
        }
        return t1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值