[抄题]:

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的结果]:

[总结]:

DC和traverse的区别就是有等号和没等号

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

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

左右讨论还是用的traverse嵌套

[关键模板化代码]:

//left & right :divide into node's left & node's right
node.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 null
if (t1 == null) {
return t2;
}
if (t2 == null) {
return t1;
}
//left.val + right.val: new val needs new node
TreeNode node = new TreeNode(t1.val + t2.val);
//left & right :divide into node's left & node's right
node.left = mergeTrees(t1.left, t2.left);
node.right = mergeTrees(t1.right, t2.right); return node;
}
}

20/05/10

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null) return t2;
if (t2 == null) return t1; TreeNode mergedNode = new TreeNode();
mergedNode.val = t1.val + t2.val;
mergedNode.left = mergeTrees(t1.left, t2.left);
mergedNode.right = mergeTrees(t1.right, t2.right); return mergedNode;
}
}

17. Merge Two Binary Trees 融合二叉树的更多相关文章

  1. 17.Merge Two Binary Trees(合并两个二叉树)

    Level:   Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...

  2. [LeetCode] 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合并二叉树 (C++)

    题目: 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 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  5. Leetcode617.Merge Two Binary Trees合并二叉树

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...

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

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

  7. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  8. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

  9. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

随机推荐

  1. 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException

    今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...

  2. Mac运维安装软件

    Maccrt软件 sudo spctl --master-disable 打开软件,复制到app,按照sn.txt输入即可 sudo spctl --master-enable crt快捷键crtl ...

  3. sqlserver并发处理,锁和事务

      本文系转载,谢谢:http://www.cnblogs.com/cxd4321/archive/2008/12/10/1351792.html     另外这个也不错 http://www.cnb ...

  4. module解析过程

    加载一个核心模块时 直接require('模块名')即可 加载一个文件模块时 直接require('绝对路径/相对路径')即可,可省略文件后缀.js. 因为如果文件不存在,将试图找文件名.js的文件 ...

  5. php redis pub/sub(Publish/Subscribe,发布/订阅的信息系统)之基本使用

    一.场景介绍 最近的一个项目需要用到发布/订阅的信息系统,以做到最新实时消息的通知.经查找后发现了redis pub/sub(发布/订阅的信息系统)可以满足我的开发需求,而且学习成本和使用成本也比较低 ...

  6. requests模块session处理cookie 与基于线程池的数据爬取

    引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/ ...

  7. ArcGIS 二次开发增加工具栏按钮时,给按钮图标设置失败

    在类的构造函数中通过下面的代码获得的bitmap为null: string bitmapResourceName = GetType().Name + ".bmp"; base.m ...

  8. Django admin 使用多个数据库

    admin是django自带的一个app,那它涉及的是对Model的所有对象进行增删改查,如果model来自多个数据库如何处理呢? 重写admin.ModelAdmin的如下几个方法就好了: clas ...

  9. DataSnap 连接池 DSServer1Disconnect

    DataSnap Server DSServer1Disconnect 这个函数什么时候执行? void __fastcall TServerContainer1::DSServer1Disconne ...

  10. umbraco

    在任意页面获取根节点 var locale = CurrentPage.Site(); 遍历根节点 @foreach (var module in CurrentPage.Site().Childre ...