17. Merge Two Binary Trees 融合二叉树
[抄题]:
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提前列出来,实现自动判断
[一句话思路]:
左边和左边融合,右边和右边融合
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 出现新的数值就要新建一个节点:以前真不知道
- 左、右子树情况不同时,分为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 融合二叉树的更多相关文章
- 17.Merge Two Binary Trees(合并两个二叉树)
Level: Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...
- [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 ...
- 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 ...
- [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 ...
- Leetcode617.Merge Two Binary Trees合并二叉树
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...
- LeetCode 617. 合并二叉树(Merge Two Binary Trees)
617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- 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 ...
随机推荐
- docker-tomcat
https://yq.aliyun.com/articles/6894 1,add命令源目录 只能从 ./ 当前目录开始,目标目录是容器的目录,不是服务器的目录 2,server.xml改的东西,里面 ...
- python 有关引用的一些问题
python 有关引用的一些问题 print id.__doc__ id(object) -> integer Return the identity of an object. This ...
- 020:Buffer Pool 、压缩页、CheckPoint、Double Write、Change Buffer
一. 缓冲池(Buffer Pool) 1.1 缓冲池介绍 每次读写数据都是通过 Buffer Pool : 当Buffer Pool 中没有用户所需要的数据时,才去硬盘中获取: 通过 innodb_ ...
- oracle 中GROUP BY的用法
转自:http://blog.csdn.net/basenet855x/article/details/6694150 问题: select item.itemnum,item.in1,item.in ...
- 如何将字符串去重复demo工具
//方法一:使用集合的indexOf方法 public static void one(){ String string="aaaaaakkkkkkmnf";//需去重复的字符串s ...
- 实例演示oracle数据块状态视图v$bh的用法二 热点块问题(转)
在实例一中,我们读取到了x$bh中的tch字段,该字段表示的该字段被读取/写入的次数,这个值在oracle的LRU算法中,是一个重要的参数,如果这个字段被访问,则该值就会增加: 1,第一次查看TCH的 ...
- Linux 之 利用Google Authenticator实现用户双因素认证
一.介绍:什么是双因素认证 双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统.双因素认证是一种采用时间同步技术的系统,采用了基于时间.事件和密钥三变量而产 ...
- 获取properties配置
1. 使用@Value @Value("${swagger.enable}") 使用Spring的PropertyPlaceholderConfigurer关联 @Val ...
- vue -Missing space before value for key 'path'vue.js解决空格报错
webpack.base.config.js文件注释掉下面的东西!! module: { rules: [ /*{ test: /\.(js|vue)$/, lo ...
- C# 6 与 .NET Core 1.0 高级编程 - C# 6 的新功能
个人原创译文,转载请注明出处.有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - What’s New in C# 6 C# 6 的新功 ...