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] Merge Two Binary Trees 合并二叉树的更多相关文章

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

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

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

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

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

  5. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

  6. LeetCode 617. 合并二叉树(Merge Two Binary Trees)

    617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...

  7. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  8. 【Leetcode_easy】617. Merge Two Binary Trees

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

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

随机推荐

  1. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  2. Go实现海量日志收集系统(二)

    一篇文章主要是关于整体架构以及用到的软件的一些介绍,这一篇文章是对各个软件的使用介绍,当然这里主要是关于架构中我们agent的实现用到的内容 关于zookeeper+kafka 我们需要先把两者启动, ...

  3. Django学习(六)---博客文章页面的超链接设置

    Django中的超链接 超链接的目标地址 href后面是目标地址 template中可以用 {% url  'app_name : url_name'   param %} app_name:应用命名 ...

  4. C++中输出流的刷新问题和 endl和 \n的区别

    <C++ Primer>第5版 P6中提到endl具有换行和刷新输出流两个作用,那么没有 endl是否还会将输出流中的内容输出到设备中,再刷新输出流呢? cout << &qu ...

  5. c字符数组

    一.PTA实验作业 题目1:统计一行文本的单词个数 1. 本题PTA提交列表 2. 设计思路 定义一个长度为1000的字符数组str[1000] 在定义 i=0,cnt=0:cnt用来记录单词的个数 ...

  6. 每日冲刺报告——Day3(Java-Team)

    第三天报告(11.4  周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...

  7. Linux下I/O多路转接之select --fd_set

    fd_set 你终于还是来了,能看到这个标题进来的,我想,你一定是和我遇到了一样的问题,一样的疑惑,接下来几个小时,我一定竭尽全力,写出我想说的,希望也正是你所需要的: 关于Linux下I/O多路转接 ...

  8. 玩转Leveldb原理及源码--拙见1

    可以说是不知天高地厚.. 可以说是班门弄斧.. 但是,我今天还就这样走了,我喜欢!!!!!! 注:后续文章,限于篇幅,不懂名词都有 紫色+下划线 超链接,有兴趣,可以查阅: 网上关于Leveldb 的 ...

  9. ruby:TypeError: 对象不支持此属性或方法

    解决办法. 1.下载对应版本 下载node.js,根据ruby版本决定下载32还是x64,我的ruby版本x64 https://npm.taobao.org/mirrors/node/v8.9.3/ ...

  10. CNN中的padding

    在使用TF搭建CNN的过程中,卷积的操作如下 convolution = tf.nn.conv2d(X, filters, strides=[1,2,2,1], padding="SAME& ...