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/

https://leetcode.com/problems/merge-two-binary-trees/discuss/104299/Java-Solution-6-lines-Tree-Traversal

https://leetcode.com/problems/merge-two-binary-trees/discuss/104301/Short-Recursive-Solution-w-Python-and-C%2B%2B

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 617. Merge Two Binary Trees 合并二叉树的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. Leetcode617.Merge Two Binary Trees合并二叉树

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...

  6. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

  7. 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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. LeetCode 2:两数相加 Add Two Numbers

    ​给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  2. Excel 快捷键

    自从转岗为项目经理之后,Excel的使用频率大大的增加了,所以需要有意识的学习一下快捷键,提升自己的动作效率. 如下实在Microsoft 官网找到的资料,记录下,以作后备查看: 一.通过键盘访问功能 ...

  3. IDEA设置方法参数列表类型自动提示

    默认情况下,IDEA的提示不够完全,可以通过以下设置,将提示功能打开的更完善. 效果如下面俩图所示

  4. (转)dnSpy 强大的.Net反编译软件

    目录 1. Debug外部引用的Dll文件2. 调试应用程序3. 修改exe文件的内容 作者:D.泡沫 一说起.net的反编译软件,大家首先想到的就是Reflector,ILSpy,dotPeek等等 ...

  5. 关于VS2017离线安装的一点扩充说明

    转自:https://www.cnblogs.com/dunitian/p/8051985.html 其实逆天不推荐自己慢慢离线,找个离线包更新下再打包更快 Key:http://www.cnblog ...

  6. 解锁云原生 AI 技能|在 Kubernetes 上构建机器学习系统

    本系列将利用阿里云容器服务,帮助您上手 Kubeflow Pipelines. 介绍 机器学习的工程复杂度,除了来自于常见的软件开发问题外,还和机器学习数据驱动的特点相关.而这就带来了其工作流程链路更 ...

  7. Logstash:Data转换,分析,提取,丰富及核心操作

    Logstash:Data转换,分析,提取,丰富及核心操作 Logstash plugins Logstash是一个非常容易进行扩张的框架.它可以对各种的数据进行分析处理.这依赖于目前提供的超过200 ...

  8. 一个JAVA应用启动缓慢问题排查 --来自jdk securerandom 的问候

    开发某个项目过程中,就需求,搭建了一套测试环境.很快完成! 后来代码中加入了许多新功能,会涉及到反复重启,然后就发现了启动特别慢.这给原本功能就不多的应用增添了许多的负担. 我决定改变这一切!找到启动 ...

  9. MySQL出现Waiting for table metadata lock的原因以及解决方法(转)

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  10. 高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)

    高性能TcpServer(C#) - 1.网络通信协议 高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP) 高性能TcpS ...