每周LeetCode算法题(六): 617. Merge Two Binary Trees
来源:互联网 发布:淘宝客服怎么做兼职 编辑:程序博客网 时间:2024/05/22 02:30
每周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; }}};
- 每周LeetCode算法题(六): 617. Merge Two Binary Trees
- [leetcode]617. Merge Two Binary Trees
- leetcode.617.Merge Two Binary Trees
- [leetcode]: 617. Merge Two Binary Trees
- [Leetcode]617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees (C++)
- LeetCode 617. Merge Two Binary Trees
- [LeetCode] 617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees
- 【Leetcode】617. Merge Two Binary Trees
- [LeetCode]617.Merge Two Binary Trees
- LeetCode: 617. Merge Two Binary Trees
- Leetcode 617. Merge Two Binary Trees
- [leetcode]617. Merge Two Binary Trees
- [Leetcode] 617. Merge Two Binary Trees
- [LeetCode]617. Merge Two Binary Trees
- leetcode 617. Merge Two Binary Trees
- 【LeetCode】617. Merge Two Binary Trees
- 常用类
- 从问题角度来思考设计模式(3)
- “Hello World”在Linux系统上的运行过程
- NKOJ 3805 距离(线性筛)
- [深度学习]卷积神经网络快速入门
- 每周LeetCode算法题(六): 617. Merge Two Binary Trees
- RMI 实现的rpc 远程过程调用 Java
- Coursera自然语言处理 Week2 笔记
- 4. Median of Two Sorted Arrays
- minikube 快速搭建k8s
- 2017/10计划
- IDEA的javaweb项目的 The origin server did not find a current representation for the target resource
- 正则表达式基础元字符
- constraintLayout