[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 ...
随机推荐
- 在centos7 ubuntu15.04 上通过bosh-lite 搭建单机环境cloudfoundry
Bosh-lite简介 bosh-lite 是一个单机部署cloudfoundry的实验性工具,用于开发人员做poc 验证.Bosh-lite目前支持仅MAC OS X和Linux系统.B ...
- Excel中用countif和countifs统计符合条件的个数 good
countif单条件统计个数 1 就以下表为例,统计总分大于(包含等于)400的人数. 2 在J2单元格输入公式=COUNTIF(I2:I22,">=400") 3 回车 ...
- MAC升级node及npm
清除node.js的cache: sudo npm cache clean -f 安装 n 工具,这个工具是专门用来管理node.js版本的,别怀疑这个工具的名字,是他是他就是他,他的名字就是 &qu ...
- 应急响应--记录一次漏洞紧急处理中意外发现的挖矿木马(Shiro反序列化漏洞和ddg挖矿木马)
背景 某公司线上服务器意外发现一个Apache Shiro 反序列化漏洞,可以直接GetShell.出于做安全的谨慎,马上出现场应急,确认漏洞.该漏洞存在在cookie字段中的rememberMe字段 ...
- python 里面的%s和%r的区别
虽然这两个占位符(pytho里叫做格式符)用法相同,但是效果却是不一样的 %s是将变量传到str()函数中,结果是将变量转化适合人阅读的格式 %r是将变量穿到repr()函数中,结果是将变量转化成适合 ...
- Centos 安装yum,安装ansible
今天使用centos安装ansible,发现域名默认安装是未注册的.提示: This system is not registered to Red Hat Subscription Manageme ...
- 使用Autolayout xib实现动态高度的TableViewCell
http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...
- 在pycharm中运行nose测试框架
之前一直在pydev上或命令行上运行nosetests. pycharm上如果运行nosetests,在看了管网后,总结果如下: 全新的pycharm: 填加完成后,打开你要的脚本,运行,即可以以no ...
- 使用Wireshark分析QQ聊天
- vue--自定义验证指令
参考文档: https://cn.vuejs.org/v2/guide/custom-directive.html https://www.cnblogs.com/ilovexiaoming/p/68 ...