【LeetCode OJ】Flatten Binary Tree to Linked List
Problem Link:
http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
The problem is asking for flatterning a binary tree to linked list by the pre-order, therefore we could flatten tree from the root. For each node, we link it with its next child in the pre-order, and leave the other child for the furture flattening. We could do this by using a stack. The python code is as follows.
class Solution:
# @param root, a tree node
# @return nothing, do it in place
def flatten(self, root):
"""
Traverse the tree in pre-order,
for each node, we link it and its previous node.
"""
# Special case
if not root:
return
# Previous node
prev = root
# Unlinked node in stack
stack = []
# Check root's children
if root.right:
stack.append(root.right)
if root.left:
stack.append(root.left)
# Link all nodes in the tree
while stack:
# Get the next pre-order node
next_right = stack.pop()
# Flatten the previous node
prev.right = next_right
prev.left = None
# Go to next pre_order node, and flatten its sub-tree
prev = next_right
# Check its sub-tree
if prev.right:
stack.append(prev.right)
if prev.left:
stack.append(prev.left)
【LeetCode OJ】Flatten Binary Tree to Linked List的更多相关文章
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 【LeetCode OJ】Balanced Binary Tree
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...
- LeetCode OJ 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 OJ: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】Flatten Binary Tree to Linked List
随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【leetcode】Flatten Binary Tree to Linked List (middle)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
随机推荐
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- (原创) cocos2dx使用Curl连接网络(客户端)
0. 环境: winxpsp3, vs2010, cocos2dx@2.1.4 1. 新建一个Helloworld工程 2. HelloworldScene.h里面重写virtual bool ccT ...
- 浏览器URL编码
jsp页面中通过请求另一个页面并通过url传递了带有中文的参数,结果在接收端获取参数时乱码了 经检查乱码现象指出新在IE浏览器中,其他浏览器火狐.chrome等不会有问题 最后的解决方式是: 手动将此 ...
- CSS3的chapter5
变形样式——transform: translate() ——指定对象的2D translation(2d平移) transform-orig ...
- 从C语言快速学PHP
PHP是解释性语言,是Web开发中常用的语言.对于web编程,建议学习时参考w3cschool的在线api手册. PHP和C语言及其相似,懂C的人只要稍加学习就能写出简单的PHP程序.以下是PHP与C ...
- 让下拉框中同时显示Key与Value
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 深入浅出设计模式——外观模式(Facade Pattern)
模式动机引入外观角色之后,用户只需要直接与外观角色交互,用户与子系统之间的复杂关系由外观角色来实现,从而降低了系统的耦合度. 模式定义外观模式(Facade Pattern):外部与一个子系统的通信必 ...
- Spring中@Async注解实现“方法”的异步调用
原文:http://www.cnblogs.com/zhengbin/p/6104502.html 简单介绍: Spring为任务调度与异步方法执行提供了注解支持.通过在方法上设置@Async注解,可 ...
- FROM_UNIXTIME()和UNIX_TIMESTAMP()函数的区别
Unix时间戳(Unix timestamp),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数. 在MySQL中如何格式化时间戳?在mysql中因为t ...
- linux split (分割文件)命令
linux split 命令 功能说明:切割文件. 语 法:split [--help][--version][-<行数>][-b <字节>][-C <字节>][- ...