[LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS
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
这个题思路就是DFS, 先左后右, 记住如果是用stack如果需要先得到左, 那么要先append右, 另外需要注意的就是用recursive方式的时候需要用一个temp来去存root.right, 否则用preorder的时候root.right就是丢了!
1. Constraints
1) can be empty
2. Ideas
DFS T: O(n) S:O(1)
用pre来存之前的node, 然后DFS
3. Code
3.1) iterable
class Solution:
def flatten(self, root):
pre, stack = None, [root]
while stack:
node = stack.pop()
if node:
if pre:
pre.left = None # dont forget to set left as None
pre.right = node
pre = node
stack.append(node.right) # stack 先进后出
stack.append(node.left)
3.2) recursive
class Solution:
def __init__(self):
self.pre = None
def flatten(self, root):
if not root: return
if self.pre:
self.pre.left = None
self.pre.right = root
self.pre = root
temp = root.right # otherwise we will lose the root.right
self.flatten(root.left)
self.flatten(root.right)
4. Test cases
1) None
2)
1
/ \
2 5
/ \ \
3 4 6
[LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS的更多相关文章
- 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 ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
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 ...
- Java for 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 ...
- 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 ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
- LeetCode 114. Flatten Binary Tree to Linked List 动态演示
把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...
随机推荐
- Elasticsearch学习之深入聚合分析三---案例实战
1. 统计指定品牌下每个颜色的销量 任何的聚合,都必须在搜索出来的结果数据中进行,搜索结果,就是聚合分析操作的scope GET /tvs/sales/_search { , "query& ...
- 题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- LeetCode 10 Regular Expression Matching(字符串匹配)
题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description '.' Matches any si ...
- RedHat 简易配置 VNC Server 与VNC View详细说明
首先下载Linux版本的VNC文件. 下载地址:http://www.realvnc.com/download/vnc/ 如:VNC-5.0.2-Linux-x86-RPM.tar.gz(其实解压出来 ...
- 【CF633H】Fibonacci-ish II 莫队+线段树
[CF633H]Fibonacci-ish II 题意:给你一个长度为n的序列$a_i$.m个询问,每个询问形如l,r:将[l,r]中的所有$a_i$排序并去重,设得到的新数列为$b_i$,求$b_1 ...
- sprint boot 配置
来源:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-to ...
- jfinal的model和record如何相互转化?
一.model转record: Model类: 1. /** * Convert model to record. */public Record toRecord() { return new Re ...
- 9.9Dajngo MTV
2018-9-9 14:53:53 mvc框架和 Django的MTV框架 框架参考 :https://www.cnblogs.com/liwenzhou/p/8296964.html 2018-9- ...
- 8.29 jQuery
2018-8-29 13:22:26 jQuery : http://www.cnblogs.com/liwenzhou/p/8178806.html 都快开学了!我得在家渡劫! 今天下午去俺弟家玩去 ...
- android 常见内存泄漏原因及解决办法
android常见内存泄漏主要有以下几类: 一.Handler 引起的内存泄漏. 在Android开发中,我们经常会使用Handler来控制主线程UI程序的界面变化,使用非常简单方便,但是稍不注意,很 ...