[抄题]:

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. 2019Falg

    2019的Flag 2018 2018年对我来说是很重要的一年. 毕业--拿到硕士学位. 工作---成功转行进入互联网行业. 有了她. 上半年忙碌于毕业的各种事情,被毕业论文折磨的要疯,顺利走完所有流 ...

  2. GNU Radio: 自定义 block 实例

    综述 本文通过在GNU Radio 中编写一个block的例子,系统介绍创建一个block的过程.该 block 的功能是可以在GRC中通过滑块(WX GUI Slider)来实时改变信号源(Sign ...

  3. c++ 异常处理(3)

    <C++编码规范与指导>一文,就已经规划着要加入这样一篇讨论 C++ 异常机制的文章了.没想到时隔几年以后才有机会把这个尾巴补完 :-). 还是那句开场白:“在恰当的场合使用恰当的特性” ...

  4. erlang学习之自定义behaviour

    behaviour是啥,看了资料做了demo以后,感觉像接口,话不多说,祭代码 R15开始,回调模型使用callback来约定,更加好理解了 test_behavior.erl -module(tes ...

  5. python-redis-pipe文件

    redis导入数据比较头疼的事情,涉及几千万,导入还是很耗时,通过生成pipe文件的方式比较快捷. python3.6.1版本 在linux环境下运行 with open("data1&qu ...

  6. FPGA的新变化

    FPGA SoC通过融合FPGA和ASIC两者的元件,跨越了灵活性和性能之间的界限.但随着它们进入高安全性.任务关键型市场,它们也面临着与标准SoC相同的问题,包括在日益复杂的器件中快速传输越来越多的 ...

  7. GOF23设计模式之观察者模式(observer)

    一.观察者模式概述 观察者模式主要用于 1 :N 的通知.当一个对象(目标对象 Subject 或 Observable)的状态变化时,它需要通知一系列对象(观察者对象 Observer),令它们做出 ...

  8. HDU 1222 Wolf and Rabbit(欧几里得)

    Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. [C++]复制构造函数、赋值操作符与隐式类类型转换

    问题:现有类A定义如下: class A{public:        A(int a)                            //构造函数        {              ...

  10. 解决: Project facet Java version 1.8 is not supported

    背景 从别处Import一个Java project之后,Eclipse提示“Project facet Java version 1.8 is not supported”. 分析 从错误的描述来看 ...