问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. UnsupportedClassVersionError的错误处理

    造成这种错误的原因是支持Tomcat运行的JDK版本与支持application运行的JDK版本不一致导致的. 解决办法: 将JDK版本改成一致. 步骤 1.Window ——> Prefere ...

  2. 关于在JSP页面识别不了EL表达式的情况

    今天在JSP页面接收Controller返回的数据user_nickname,使用EL表达式显示数据发现在页面输出的始终是字符串${user_nickname} 经过查阅资料,问题在于使用的web.x ...

  3. 第六章:Android的Drawable

    Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念. 6.1 Drawable简介 Drawable常被用来作为View的背景使用. Drawable一般都是通过XML来定义的. D ...

  4. P1050 精卫填海

    [问题描述] 发鸠之山,其上多柘木.有鸟焉,其状如乌,文首,白喙,赤足,名曰精卫,其名自詨.是炎帝之少女,名曰女娃.女娃游于东海,溺而不返,故为精卫.常衔西山之木石,以堙于东海.——<山海经&g ...

  5. JavaScript经典实例(浏览器事件)

    跨浏览器事件 1.跨浏览器添加事件 function addEvent(obj,type,fn){ if(obj.addEventListener){ obj.addEventListener(typ ...

  6. 【线性表基础】基于线性表的简单算法【Java版】

    本文描述了基于线性表的简单算法及其代码[Java实现] 1-1 删除单链表中所有重复元素 // Example 1-1 删除单链表中所有重复元素 private static void removeR ...

  7. 2020数字中国创新大赛虎符网络安全赛道-pwn count

    比赛结束前半个小时才看的题,等我做出来比赛已经结束了.难受Orz 本地文件无法执行,远程调试. 题目大概意思就是让你计算200道四则运算.(实际上格式是固定的.先乘一次然后再加两次).200道题都正确 ...

  8. Google免费新书-《构建安全&可靠的系统》

    前段时间riusksk在公众号分享的Google安全团队的新书,好书,全英原版,开源免费. 免费下载地址:https://static.googleusercontent.com/media/land ...

  9. Spring发布WebService并调用已有的WebService

    发布WebService 1.编写生成WebService的Java类 package com.webService; import com.service.PianoServiceImpl; imp ...

  10. 解释Crypto模块,No module named "Crypto"

    1.pip install pycryptodome 2.Python\Python38\Lib\site-packages,找到这个路径,下面有一个文件夹叫做crypto,将小写c改成大写C就ok了 ...