[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要从上往下讨论是否有空节点:实际上是讨论不出来的,特殊情况要当作corner case提前列出来,实现自动判断

[一句话思路]:

左边和左边融合,右边和右边融合

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 出现新的数值就要新建一个节点:以前真不知道
  2. 左、右子树情况不同时,分为node.left 和node.right两边去讨论就行了,第二次见了应该学会了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

左右讨论还是用的traverse嵌套

[关键模板化代码]:

//left & right :divide into node's left & node's rightnode.left = mergeTrees(t1.left, t2.left);node.right = mergeTrees(t1.right, t2.right);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {//corner case:left is null or right is nullif (t1 == null) {return t2;}if (t2 == null) {return t1;}//left.val + right.val: new val needs new nodeTreeNode node = new TreeNode(t1.val + t2.val);//left & right :divide into node's left & node's rightnode.left = mergeTrees(t1.left, t2.left);node.right = mergeTrees(t1.right, t2.right);return node;}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/8566678.html

17. Merge Two Binary Trees 融合二叉树相关推荐

  1. C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...

  2. 617.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 | 617. 合并二叉树(Java)

    题目 https://leetcode-cn.com/problems/merge-two-binary-trees/ 题解 测试用例 输入: [1,3,2,5] [2,1,3,null,4,null ...

  5. 【Tree-easy】617. Merge Two Binary Trees 合并两个二叉树

    1. 题目原址 https://leetcode.com/problems/merge-two-binary-trees/ 2. 题目描述 3. 题目大意 给定两个二叉树,现在要求将这两个二叉树合并, ...

  6. 3/100. Merge Two Binary Trees

    将两个二叉树相同位置的数值相加,相加的方法一样,则使用递归法挨个相加即可. # Definition for a binary tree node. # class TreeNode: # def _ ...

  7. [leetcode] 617. Merge Two Binary Trees

    题目描述 给定两棵树,要求合并它们,如果节点重合则新合并的树的节点为它们之和,否则不变. 思路 简单递归,c语言怎么创建新节点忘记了,就直接写的. 代码 /*** Definition for a b ...

  8. Given two binary trees, write a function to check if they areequal or not.

    Given two binary trees, write a function to check if they areequal or not. Two binary trees are cons ...

  9. (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……

    当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...

最新文章

  1. CentOS 命令提示符颜色及样式详解
  2. JS 监控页面刷新,关闭 事件的方法(转载)
  3. 对静态区,栈,堆的理解
  4. .NET Framework开源详细配置
  5. oracle中case when关键字的使用
  6. 年龄大了学Java是爱好还是转型?
  7. 【网络】tcp三次握手协议
  8. s3c6410 ddr初始化
  9. php 参数 问号_php获取不到url问号之后的参数
  10. 关于IE的RegExp.exec
  11. [转载]无线通信系统中的调制解调基础(一):AM和FM
  12. 一个demo学会jquery mobile
  13. 如何在Linux中删除符号链接?
  14. matlab 线性拟合polyfit_matlab如何做线性拟合
  15. 中继器是什么计算机网络,中继器是什么
  16. MVX Android设计架构浅析-MVC
  17. 2020中国公关公司30强排行榜(记录)
  18. 纯CSS实现四种方式文本反差色效果
  19. Network Password Recovery工具查看windows凭据密码
  20. hdu 5234 动态规划

热门文章

  1. k8s Service之LoadBalancer和ExternalName
  2. AtomicLong与LongAdder执行效率对比
  3. Scala模式匹配:对象匹配
  4. Scala隐式转换之隐式类
  5. yarn资源管理调度平台
  6. JVM 调优实战--jvisualvm远程连接使用教程
  7. Struts2框架完成登录操作案例
  8. 【示例】Lucene查询索引库编程步骤
  9. IDEA创建java文件失败,但是new选项中有java class选项,设置中file and Code Templates中有对应模板
  10. mysql三次握手_TCP的三次握手和四次挥手详解