问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4096 访问。

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

输入: 

Tree 1                     Tree 2                  

          1                         2                             

         / \                       / \                            

        3   2                     1   3                        

       /                           \   \                      

      5                             4   7                  
输出: 

合并后的树:

         3

        / \

       4   5

      / \   \ 

     5   4   7

注意: 合并必须从两个树的根节点开始。


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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4096 访问。

public class Program {

    public static void Main(string[] args) {
var t1 = new TreeNode(1) {
left = new TreeNode(3)
}; var t2 = new TreeNode(2) {
left = new TreeNode(5) {
left = new TreeNode(7),
right = new TreeNode(9)
},
right = new TreeNode(6)
}; var res = MergeTrees(t1, t2);
ShowTree(res);
Console.WriteLine(); res = MergeTrees2(t1, t2);
ShowTree(res); Console.ReadKey();
} public static void ShowTree(TreeNode node) {
if(node == null) {
Console.Write("null ");
return;
}
Console.Write($"{node.val} ");
ShowTree(node.left);
ShowTree(node.right);
} public static TreeNode MergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null && t2 == null) return null;
var root = new TreeNode(0);
Merge(t1, t2, ref root);
return root;
} public static void Merge(TreeNode t1,
TreeNode t2,
ref TreeNode root) {
if(t1 == null && t2 == null) return;
if(t1 == null) {
root = new TreeNode(t2.val);
} else if(t2 == null) {
root = new TreeNode(t1.val);
} else {
root = new TreeNode(t1.val + t2.val);
}
Merge(t1?.left, t2?.left, ref root.left);
Merge(t1?.right, t2?.right, ref root.right);
} public static TreeNode MergeTrees2(TreeNode t1, TreeNode t2) {
if(t1 != null && t2 != null) {
t1.val = t1.val + t2.val;
t1.left = MergeTrees2(t1.left, t2.left);
t1.right = MergeTrees2(t1.right, t2.right);
return t1;
}
if(t1 == null) return t2;
if(t2 == null) return t1;
return null;
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4096 访问。

3 8 7 null null 9 null null 6 null null
3 8 7 null null 9 null null 6 null null

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)的更多相关文章

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

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

  2. [Swift]LeetCode617. 合并二叉树 | 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. C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4074 访问. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本 ...

  4. C#LeetCode刷题之#226-翻转二叉树(Invert Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4080 访问. 翻转一棵二叉树. 输入: 4    /   \   ...

  5. LeetCode刷题笔记-递归-反转二叉树

    题目描述: 翻转一棵二叉树. 解题思路: 1.对于二叉树,立马递归 2.先处理 根节点,不需改动 3.处根的左子树和右子树需要交换位置 4.递归处理左子树和右子树.步骤见1-3步 Java代码实现: ...

  6. C#LeetCode刷题之#101-对称二叉树(Symmetric Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4068 访问. 给定一个二叉树,检查它是否是镜像对称的. 例如,二 ...

  7. 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  8. 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...

  9. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

随机推荐

  1. ubuntu 下安装QQ TIM QQ轻聊版 微信 Foxmail 百度网盘 360压缩 WinRAR 迅雷极速版

    第1步,安装deepin-wine环境:上https://github.com/wszqkzqk/deepin-wine-ubuntu页面下载zip包(或用git方式克隆),解压到本地文件夹,在文件夹 ...

  2. 集训作业 洛谷P1010 幂次方

    这个…… 这个题看上去有点难的样子. 仔细看看,感觉有点简单.啊,是递归啊,正经的看一看,好像是把一个数分成2的几次方的和. 然后余数和比他小的最大的2的次方数如果不是2的一次方或者2的0次方,就继续 ...

  3. Html笔试复习

    掌握学习技巧,提高学习质量 学习目标:熟练掌握Html笔试复习题 已掌握目标:Html笔试复习题掌握95% 未完成目标:个别题因粗心造成错误,因选项意思不懂出错 解决方案:了解原因,因错出方案,充分利 ...

  4. web自动化 -- 三种等待方式

    一.强制等待 二.隐式等待 注:隐式等待的作用域是全局,所以一般设置在整局代码的头几行. 如: 三.显示等待 元素存在: 元素可见: 元素可点击: 看到上图源码中有一个   element.is_en ...

  5. Java基础之java8新特性(1)Lambda

    一.接口的默认方法.static方法.default方法. 1.接口的默认方法 在Java8之前,Java中接口里面的默认方法都是public abstract 修饰的抽象方法,抽象方法并没有方法实体 ...

  6. Java+MySQL企业级实训全套课程

    总纲 JAVA基础部分 教学视频:第一讲:Java入门与环境搭建    提取码:h9vm第二讲:变量与运算符    提取码:928t第三讲:顺序结构及条件结构    提取码:3v1l第四讲:while ...

  7. MyBatis----resultMap的使用

  8. Linux系统zabbix_agentd客户端安装与配置

    标注:官网下载zabbix安装包(zabbix安装包里包含了zabbix_agentd客户端安装包,我们只选择zabbix_agentd客户端安装) zbbix官网下载地址:  http://www. ...

  9. Day02_CMS前端开发

    CMS前端开发 1 Vue.js与Webpack研究 参考链接:https://www.cnblogs.com/artwalker/p/13387386.html 2 CMS前端工程创建 2.1 导入 ...

  10. MacOS下Nginx安装

    1. 先安装homebrew 2. 安装Nginx,终端下执行: $ brew install nginx 安装过程中会自己安装依赖: 3. 启动nginx服务 $ nginx 成功后,使用浏览器打开 ...