每周LeetCode算法题(六): 617. Merge Two Binary Trees

每周LeetCode算法题(六)

题目: 617. Merge Two Binary Trees

Given two binary trees and 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 them 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 new tree.

Example 1:
Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/ \
4 5
/ \ \
5 4 7
Note: The merging process must start from the root nodes of both trees.

解法分析

题目的要求是,给定两棵树,建立一个每个位置上值为它们对应位置上的值的和的树。若其中一棵树的一个位置上为空,则当作0处理。

我的做法是同时先序遍历两棵树,同时求和并建一棵新树,这样能不伤及旧树。先序遍历旧树加新树每个用一个栈。其余剩下的就基本和先序遍历一致了。

注意,如果一个节点上,一棵树有值,一棵树为空,那么对应于后者的那个栈就加入一个值为0、无后继的节点作为替代,这样不会影响求和。

C++代码

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        stack<TreeNode*> st1, st2, sa;
        TreeNode* ans = new TreeNode(0);
        TreeNode* em = new TreeNode(0);
        TreeNode* a = ans;
        while (!sa.empty() || (t1 || t2)) {
            while (t1 || t2) {
                if (t1 && t2) {
                    a->left = new TreeNode(t1->val + t2->val);
                    st1.push(t1);
                    st2.push(t2);
                    t1 = t1->left;
                    t2 = t2->left;
                } else if (t1 && !t2) {
                    a->left = new TreeNode(t1->val);
                    st1.push(t1);
                    st2.push(em);
                    t1 = t1->left;
                    t2 = NULL;
                } else if (!t1 && t2) {
                    a->left = new TreeNode(t2->val);
                    st1.push(em);
                    st2.push(t2);
                    t1 = NULL;
                    t2 = t2->left;
                }
                a = a->left;
                sa.push(a);
            }
            t1 = st1.top()->right;
            st1.pop();
            t2 = st2.top()->right;
            st2.pop();
            a = sa.top();
            sa.pop();
            if (t1 || t2) {
                if (t1 && t2) {
                    a->right = new TreeNode(t1->val + t2->val);
                    st1.push(t1);
                    st2.push(t2);
                    t1 = t1->left;
                    t2 = t2->left;
                } else if (!t2) {
                    a->right = new TreeNode(t1->val);
                    st1.push(t1);
                    st2.push(em);
                    t1 = t1->left;
                    t2 = NULL;
                } else if (!t1) {
                    a->right = new TreeNode(t2->val);
                    st1.push(em);
                    st2.push(t2);
                    t1 = NULL;
                    t2 = t2->left;
                }
                a = a->right;
                sa.push(a);
            }
        }
        return ans->left;
    }
};

另一种解法

题目的标准解法如下,但这是一种会改变原先树结构的做法。

public class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1 == null)
            return t2;
        Stack < TreeNode[] > stack = new Stack < > ();
        stack.push(new TreeNode[] {t1, t2});
        while (!stack.isEmpty()) {
            TreeNode[] t = stack.pop();
            if (t[0] == null || t[1] == null) {
                continue;
            }
            t[0].val += t[1].val;
            if (t[0].left == null) {
                t[0].left = t[1].left;
            } else {
                stack.push(new TreeNode[] {t[0].left, t[1].left});
            }
            if (t[0].right == null) {
                t[0].right = t[1].right;
            } else {
                stack.push(new TreeNode[] {t[0].right, t[1].right});
            }
        }
        return t1;
    }
}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值