问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. C#LeetCode刷题之#110-平衡二叉树(Balanced Binary Tree)

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

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

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

  3. leetcode刷题记录总结-7.二叉树

    文章目录 零.二叉树理论 二叉树的种类 满二叉树 完全二叉树 二叉搜索树 平衡二叉搜索树 二叉树的存储方式 二叉树的遍历方式 二叉树的定义 总结 一.二叉树的遍历 [144. 二叉树的前序遍历 ](h ...

  4. ​LeetCode刷题实战314:二叉树的竖直遍历

    算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...

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

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

  6. 【LeetCode刷题】23. 合并K个排序链表

    23. 合并K个排序链表 题目 思路1 - 普通思路 思路2 - 逐一比较 思路3 – 逐一两两合并 思路4 – 优先级队列(小顶堆) 思路5 – 分治策略 题目 链接:23. 合并K个排序链表 合并 ...

  7. LeetCode刷题Java——88.合并两个有序数组(简单)

    题目 给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目. 请你合并nums2到nums1中,使合并后的数组同样按非递减顺序排列. ...

  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 验证二叉搜索树 22.2% ...

最新文章

  1. TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议
  2. Vista及Win7常见故障(拷贝)
  3. 支持javascript的ppt软件_github重磅推荐!一个很好用的PPT生成工具
  4. linux精简、备份还原、iso文件、批量安装?
  5. (论文阅读笔记1)Collaborative Metric Learning(二)(WWW2017)
  6. Screaming Frog SEO Spider for Mac(网络爬虫软件)v16.0
  7. GJB289A总线测试工装研究
  8. java 按拼音模糊搜索汉字_C#中拼音模糊匹配汉字智能搜索
  9. 服务器声卡硬件安装,win 2008虚拟声卡的配置
  10. ERP系统-应收应付子系统-付款单
  11. ps人物碎片化飞溅效果特效怎么做
  12. python图片查看器
  13. (转)NET Reflector简介
  14. C++项目实战——简易版日期计算器(Date类改编)
  15. 复杂系统理论解释了Covid为何粉碎世界
  16. Springboot测试类之@RunWith注解
  17. 如何解决算百分比,加起来不是100%的问题
  18. 港台明星们的生日大曝光
  19. 第04课: wiki 在 GitHub
  20. 程序员 贼好用的软件推荐

热门文章

  1. 综合演练 实现登陆功能 1124
  2. python导出mysql授权语句
  3. BOM函数之history对象
  4. C# MVC中过滤器的简单使用
  5. DirectX - dds图片格式(DDSURFACEDESC2)
  6. SOFA 源码分析 — 扩展机制
  7. 从Softmax回归到Logistic回归
  8. 基于jquery类库的绘制二维码的插件jquery.qrcode.js
  9. 016医疗项目 数据字典(概念和在本系统中的应用)
  10. 启动另一个activity