【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 ...
随机推荐
- 右值引用、move与move constructor
http://blog.chinaunix.net/uid-20726254-id-3486721.htm 这个绝对是新增的top特性,篇幅非常多.看着就有点费劲,总结更费劲. 原来的标准当中,参数与 ...
- JAVA基础知识之NIO.2——Path,Paths,Files
NIO.2 JDK7对NIO进行了重大改进,主要包含以下两方面 新增Path接口,Paths工具类,Files工具类. 这些接口和工具类对NIO中的功能进行了高度封装,大大简化了文件系统的IO编程. ...
- Flowplayer-encoding
SOURCE URL: https://flowplayer.org/docs/encoding.html Video encoding To ease the task of encoding yo ...
- 编译android源码官方教程(6)编译内核
Building Kernels IN THIS DOCUMENT Selecting a kernel Identifying kernel version Downloading sources ...
- <转>修改TM2013聊天记录保存目录final版
一直很钟意TM的清爽,然而在升级到TM2013 preview1后,发生了一件很让人头痛的事. 那就是无法设定消息目录,TM会默认为保存在我的文档下.这让使用dropbox同步聊天记录的我感到十 ...
- Hosts文件的使用
hosts文件是什么?在哪里?hosts文件:系统文件,可以记事本打开并编辑.一般用于域名到ip地址的解析.当用户在浏览器中输入网络的域名时,系统首先会自动从hosts文件中找到对应的ip地址,一旦找 ...
- jq 移除包含某个字符串的类名js
el.removeClass (function (index, css) { return (css.match (/(^|\s)star\S+/g) || []).join(' ');//移除以“ ...
- 深入浅出设计模式——模板方法模式(Template Method Pattern)
模式动机 模板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一.在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中.在模板方法模式 ...
- XMPP学习记录之实战篇
在学习iOS以来一直想要研究即时聊天方面的技术,因工作原因此计划一直搁浅,近日偷得时闲开始着手与XMPP的学习.在学习之前我一直认为XMPP对我来说是一个很有难度的挑战,在了解了协议的具体形式后,才发 ...
- java实现httpclient2
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...