114. Flatten Binary Tree to Linked List 把二叉树变成链表
[抄题]:
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道大概,以为要用helper函数,其实可以不用,直接在主函数中写
[英文数据结构或算法,为什么不用别的数据结构或算法]:
tree中按正常的顺序prev.right = root连总会返回原来的tree,+右中左两次取反才能形成中序链表
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
tree中按正常的顺序prev.right = root连总会返回原来的tree,+右中左两次取反才能形成中序链表
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
TreeNode prev = null;
public void flatten(TreeNode root) {
//when to exit
if (root == null) return ;
//r, l , reverse
flatten(root.right);
flatten(root.left);
root.right = prev;
root.left = null;
prev = root;
}
}
114. Flatten Binary Tree to Linked List 把二叉树变成链表的更多相关文章
- [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- 114 Flatten Binary Tree to Linked List [Python]
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- 【LeetCode】114. Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
随机推荐
- 1.1.19 Word中表格自动断开
1.修改前效果如下图所示: 2.先右键点击表格的左上角的“被正方形包着的四方箭头”, 如下图中的序号1,在出现的快捷菜单上点击[表格属性],出现[表格属性]对话框. 3.将参数设置成“允许跨页断行”, ...
- 1.1.17 Word在表格中插入竖排文字,显示一半
隐藏效果如下所示: 这是因为文字的[段落行距]设置为[固定值],将文字选中,设置为[单倍行距]即可.
- [C#]typeof,Gettype()和is的区别
typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...
- mac os high sierra下搭建php多版本-php5.2+php5.6-nginx
xampp的apache彻底启动不来了. php52的编译参数 ./configure --prefix=/usr/local/Cellar/php52bysk/ --with-config-file ...
- 关于Verilog中begin-end & fork-join
转载:http://blog.sina.com.cn/s/blog_6c7b6f030101cpgt.html begin-end and fork-join are used to combi ...
- Delphi 7升级到XE2的字符串问题
原来的Delphi中有两种字符串:AnsiString和WideString.默认的string即AnsiString.而在Delphi 2009中,新增加了一种UnicodeString.为什么不沿 ...
- git clone git@github.com:xxx.git Permission denied (publickey) 问题解决办法
From: https://www.cnblogs.com/restart/p/4633928.html 如果git无法通过普通的http去clone远程分支,可以选用ssh方式去连接.这时需要配置相 ...
- 利用飞儿云PHP框架自带的DNSPOD库做DDNS动态域名解析
取得FiradioPHP git clone https://github.com/firadio/firadiophp.git 保存到/config/dnspod1.php <?php ret ...
- python学习快人一步,从19个语法开始!
Python简单易学,但又博大精深.许多人号称精通Python,却不会写Pythonic的代码,对很多常用包的使用也并不熟悉.学海无涯,我们先来了解一些Python中最基本的内容. Python的特点 ...
- java 性能测试框架工具-junitperf
性能测试工具 对于 Java 开发者来说,要去学习性能测试工具未免很麻烦. 但有时候会有性能测试的需求. junitperf junitperf 就是一款为 Java 开发者设计的性能测试框架,如果你 ...