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. 如何配置jdk的本地环境

    在计算机→属性→高级系统设置→高级→环境变量,如图: 第一步:系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是C:\Program Files\Java\jdk1.8. ...

  2. 用Java开发的【智能语音开发板MEGA ESP32AI】

    有点激动 ~ ~ ~ 新鲜出炉,用视频看看效果哦 我们新研发出世的语音开发板MEGA ESP32AI,来看看吧,有点腻害哦!!!先演示下功能语音控制开关等.播报天气 戳下面链接看视频哦? MEGA E ...

  3. 【转】Git GUI基本操作

    一.Git GUI基本操作 1.版本库初始化 gitpractise文件夹就变成了Git可以管理的仓库,目录下多了一个.git文件夹,此目录是Git用于管理版本库的,不要擅自改动里面的文件,这样会破坏 ...

  4. Knative 实践:从源代码到服务的自动化部署

    通过之前的文章,相信大家已经熟悉了 Serving.Eventing 以及 Tekton.那么在实际使用中,我们往往会遇到一些复杂的场景,这时候就需要各个组件之间进行协作处理.例如我们提交源代码之后是 ...

  5. OpenGL入门1.2:渲染管线简介,画三角形

    每一个小步骤的源码都放在了Github 的内容为插入注释,可以先跳过 图形渲染管线简介 在OpenGL的世界里,任何事物是处于3D空间中的,而屏幕和窗口显示的却是2D,所以OpenGL干的事情基本就是 ...

  6. dubbo入门学习

    官方网址:http://dubbo.apache.org/zh-cn/index.html 学习可以参考官网中文文档:http://dubbo.apache.org/zh-cn/docs/user/q ...

  7. Asp.Net Core Mvc Razor之RazorPage

    在AspNetCore.Mvc.Razor命名空间中的RazorPage继承RazorPageBase,并定义的属性为: HttpContext Context 表示当前请求执行的HttpContex ...

  8. Ribbon架构剖析

    在学习Ribbon之前,先看一下这张图,这张图完美的把Ribbon的基础架构给描述出来了 这张图的核心是负载均衡管理器,围绕着它的是外面的这5大功能点,咱们就从核心开始看然后再带出来其他的功能 首先看 ...

  9. 谁在使用GPU?

    nvidia-smi命令可以查看GPU使用情况,但是只能看到占用每个GPU的进程ID.根据进程ID可以得到进程详情,进程详情中包括用户ID,根据用户ID可以获取用户名称,从而知道哪个用户在使用GPU. ...

  10. 章节十四、9-Actions类鼠标悬停、滚动条、拖拽页面上的元素

    一.鼠标悬停 1.在web网站中,有一些页面元素只需要我们将鼠标指针放在上面就会出现被隐藏的下拉框或者其它元素,在自动化的过程中我们使用Actions类对鼠标进行悬停操作. 2.案例演示 packag ...