问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. SQL注入环境的搭建

    使用Phpstudy搭建SQL注入环境: 1.下载phpstudy安装 2.下载sql实验环境 所用环境的代码是一个印度人的开源项目平台.里面包含了基本的各种注入类型,同时又有get和post类型,以 ...

  2. day3 python数据类型转换及变量的缓存机制

    类型转换 1,强制类型转换 1.1 number的转换(int,float,bool,complex) num1 = 10 num2 = 10.6 num3 = True num4 = 3 + 4j ...

  3. Event-Driven Architecture思考

    什么是Event? An event represents a fact, something happened; and it is immutab. 事件代表着事实,代表着过去发生的某件事情,是不 ...

  4. Ubuntu构建Docker私有仓库(Repository) 配置过程笔记

    一.准备: 1.服务器(或者虚拟机2台,我的服务环境[  阿里云服务器-Ubuntu 1804 +百度云-Ubuntu 1604]) 2.有效镜像(我这里以上一篇随笔镜像作为有效镜像https://w ...

  5. Lucas定理 & Catalan Number & 中国剩余定理(CRT)

    又双叒叕来水数论了 今天来学习\(Lucas \:\ \& \:\ Catalan Number\) 两者有着密切的联系(当然还有CRT),所以放在一起学习一下 \(Lucas\) 定义\(\ ...

  6. 性能测试必备知识(5)- 深入理解“CPU 上下文切换”

    做性能测试的必备知识系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1806772.html 前言 上一篇文章中,举例了大量进程等待 CP ...

  7. socket网络(二)

    作用域 python/js语言中,无块级作用域 if 1 == 1: name = 'alex' print(name) python中以函数为作用域 def func(): name = 'alex ...

  8. Druid 连接池

    记录Druid 连接池简单用法 package Utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sq ...

  9. java JDBC自我总结

    preparedstatement和statement的区别 当不需要预编译时(不需要占位符)可以选用statement,存在不安全 当有占位符(?)时,需要选用preparedstatement s ...

  10. 容器centos7安装部署ansible

    容器centos7安装部署ansible centos镜像版本及ansible版本 centos:centos7.5.1804 ansible:2.9.11 启动容器并进入容器 docker run ...