【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 ...
随机推荐
- webapi 解决ajax跨域请求问题
webapi在配置文件中加入这几句就可以解决ajax(同源策略是JavaScript里面的限制,其他的编程语言,比如在C#,Java或者iOS等其他语言中是可以调用外部的WebService,也就是 ...
- sd 卡驱动--基于高通平台
点击打开链接 内容来自以下博客: http://blog.csdn.net/qianjin0703/article/details/5918041 Linux设备驱动子系统第二弹 - SD卡 (有介绍 ...
- 14.KVM安装之脚本和镜像目录树准备
1.php脚本需要先安装PHP环境,Apache服务器必须支持PHP $ yum install -y php #安装PHP $ php -v #查看是 ...
- UVa 12299 RMQ with Shifts(移位RMQ)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- union (共用声明和共用一变量定义)
"联合"是一种特殊的类,也是一种构造类型的数据结构.在一个"联合"内可以定义多种不同的数据类型, 一个被说明为该"联合"类型的变量中,允许装 ...
- java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值
1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...
- 响应式 css
1.class 样式一般用class,命名:中横线分隔,如:div-logo id 一般用于js快速地区别和获取元素,命名:驼峰命名法,如:divLogo (中间首字母大写) 2.必不可少的图片,用& ...
- 鼠标滑过弹出jquery在线客服
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 在centos中安装jenkins master测试环境
在centos中安装jenkins 1)安装目录 pwd (/home/AAA) 2)检查java是否安装 [AAA@Centos_AAA jenkins]$ java -version j ...
- Python--关于连接符+
连接符 + 连接符 + 实则是创建了新的对象并占用新的内存(dict.set不能使用) String 由于Python必须为每一个使用连接符+的字符串分配新的内存,并产生新的字符串.下面两种方式会更有 ...