[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.
这道题给了两个二叉树,让我们合并成一个,规则是,都存在的结点,就将结点值加起来,否则空的位置就由另一个树的结点来代替。那么根据过往经验,处理二叉树问题的神器就是递归。根据题目中的规则,如果要处理的相同位置上的两个结点都不存在的话,直接返回即可,如果 t1 存在,t2 不存在,就以 t1 的结点值建立一个新结点,然后分别对 t1 的左右子结点和空结点调用递归函数,反之,如果 t1 不存在,t2 存在,就以 t2 的结点值建立一个新结点,然后分别对 t2 的左右子结点和空结点调用递归函数。如果 t1 和 t2 都存在,就以 t1 和 t2 的结点值之和建立一个新结点,然后分别对 t1 的左右子结点和 t2 的左右子结点调用递归函数,参见代码如下:
解法一:
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
TreeNode *res = NULL;
helper(t1, t2, res);
return res;
}
void helper(TreeNode* t1, TreeNode* t2, TreeNode*& res) {
if (!t1 && !t2) return;
else if (t1 && !t2) {
res = new TreeNode(t1->val);
helper(t1->left, NULL, res->left);
helper(t1->right, NULL, res->right);
} else if (!t1 && t2) {
res = new TreeNode(t2->val);
helper(NULL, t2->left, res->left);
helper(NULL, t2->right, res->right);
} else {
res = new TreeNode(t1->val + t2->val);
helper(t1->left, t2->left, res->left);
helper(t1->right, t2->right, res->right);
}
}
};
其实远不用写的像上面那么复杂,连额外的函数都不用写,直接递归调用给定的函数即可,首先判断,如果 t1 不存在,则直接返回 t2,反之,如果 t2 不存在,则直接返回 t1。如果上面两种情况都不满足,那么以 t1 和 t2 的结点值之和建立新结点t,然后对 t1 和 t2 的左子结点调用递归并赋给t的左子结点,再对 t1 和 t2 的右子结点调用递归并赋给t的右子结点,返回t结点即可,参见代码如下:
解法二:
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (!t1) return t2;
if (!t2) return t1;
TreeNode *t = new TreeNode(t1->val + t2->val);
t->left = mergeTrees(t1->left, t2->left);
t->right = mergeTrees(t1->right, t2->right);
return t;
}
};
Github:
https://github.com/grandyang/leetcode/issues/617
参考资料:
https://leetcode.com/problems/merge-two-binary-trees/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 617. Merge Two Binary Trees 合并二叉树的更多相关文章
- LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)
题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- [LeetCode] Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- 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 ...
- Leetcode 617 Merge Two Binary Trees 二叉树
题意: 给定两棵树,将两棵树合并成一颗树 输入 Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出 合并的树 3 / \ 4 5 / \ \ 5 4 7 ...
- Leetcode617.Merge Two Binary Trees合并二叉树
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
- LeetCode 617. Merge Two Binary Tree (合并两个二叉树)
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- 【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 o ...
随机推荐
- github clone加速
1. 在https://asm.ca.com/zh_cn/ping.php 网址中查询 github.global.ssl.fastly.net 及 github.com 的 china地区 avr ...
- skeleton在心意web上的实践
通过手动编写skeleton,在fetch数据时显示skeleton loading,数据拉取成功隐藏skeleton 先看下效果图 在component下创建页面对应的skeleton,然后通过在i ...
- Winform 窗体皮肤美化_IrisSkin
1 先把IrisSkin2.dll文件添加到当前项目引用(解决方案资源管理器->当前项目->引用->右键->添加引用,找到IrisSkin2.dll文件.....之后就不用我说 ...
- [翻译]微软 Build 2019 正式宣布 .NET 5
原文: Introducing .NET 5 今天,我们宣布 .NET Core 3.0 之后的下一个版本将是 .NET 5 .这将是 .NET 系列的下一个重要版本. 将来只会有一个 .NET ,您 ...
- Git多账号配置
在一台电脑上配置多个不同的 ssh key 前言 如果拥有多个Git远程仓库,尤其是其中一个是工作中使用的仓库,只使用一个ssh key安全性很低,建议为不同Git远程仓库配置不同的ssh key. ...
- python实现罗汉塔破解方法
主要使用函数的递归方法,考虑过程如下:n,a,b,c(n代表罗汉塔块数,a,b,c代表三块柱子)若n=1时,只需从a>>>c若n>1时,需要把上面n-1块从a移动到b,底下1块 ...
- 达能依靠Matrikon进行数据存储和分析
达能是一家致力于通过食品实现健康的公司,业务遍及五大洲130多个国家.在罗马尼亚,达能每天在布加勒斯特的工厂生产100万瓶酸奶.由于质量和产品安全在达能最为重要,因此监控和分析工业过程需要非常高效才能 ...
- vue--购物车案例(小知识点总结)
Html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- JAVA中为什么要配置环境变量?怎么配置环境变量?
1.为什么要配置环境变量? 答:为了让javac命令(编译命令)和Java命令(运行命令)能在任何文件夹都能运行. 2.怎么配置环境变量? JAVA_HOME : D:\develop\Java\jd ...
- Python元组与字符串操作(8)——三数排序多种实现
练习 依次接收用户输入的3个数,排序后打印 1.转换int后,判断大小排序,使用分支结构完成 num1 = [] for i in range(3): num1.append(int(input('& ...