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 ...
随机推荐
- 全志A33 lichee lvds屏幕配置
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 芯灵思SinlinxA33开 ...
- websocket 11
1. websocket 回顾: - 什么是轮训? - 通过定时器让程序每隔n秒执行一次操作. - 什么是长轮训? - 浏览器向后端发起请求,后端会将请求 hang 住,最多hang 30s. 如果一 ...
- Dart Map<> 添加 元素
Map<String, WidgetBuilder> routesList() { Map<String, WidgetBuilder> re = new Map<Str ...
- 对象属性的描述:writable、enumerable、configurable
writable属性 writable属性是一个布尔值,决定了目标属性的值(value)是否可以被改变.如果原型对象的某个属性的writable为false,那么子对象将无法自定义这个属性. enum ...
- Day 13 可迭代对象,迭代器对象,for循环迭代,生成器对象,枚举对象
一.迭代器概念:# 器:包含了多个值的容器# 迭代:循环反馈(一次从容器中取出一个值)# 迭代器:从装有多个值的容器中一次取出一个值给外界# ls = 'abcdef'ls = [1, 2, 3, 4 ...
- InnoDB存储引擎文件
InnoDB存储引擎文件 MySQL数据库包括数据库本身的文件和存储引擎文件.数据库自身的文件由参数文件(my.cnf).错误日志文件.慢查询日志文件.查询日志文件.二进制日志文件.套接字文件.pid ...
- GradleUserGuide中文版 19)Plugins 20)插件规范 21)Java插件
https://blog.csdn.net/roymuste/article/details/51321881
- lavarel 中间件
创建中间件 php artisan make:policy UserPolicy 所有生成的授权策略文件都会被放置在 app/Policies 文件夹下. 让我们为默认生成的用户授权策略添加 upda ...
- windows下缩短time_wait的时间
最近线上遇到windows机器访问其他机器的时候失败的情况.实际就是本地的端口不够用造成的. D:\>netsh interface ipv4 show dynamicportrange pro ...
- 【Servlet】(1)Servlet简介、Servlet底层原理、Servlet实现方式、Servlet生命周期
一.Servlet简介 1.Servlet定义: Servlet(Server Applet)是Java Servlet的简称,是为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交 ...