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的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. 【LeetCode OJ】Balanced Binary Tree

    Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...

  4. 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 ...

  5. 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 ...

  6. 【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 ...

  7. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  8. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  9. 【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 ...

随机推荐

  1. Mac平台上OpenCV开发环境搭建

    转载于:https://segmentfault.com/a/1190000000711132 linux 编译指定库.头文件的路径问题 http://blog.csdn.net/jiaweizou/ ...

  2. 探究platform_driver中的shutdown用途

    http://blog.csdn.net/moxiaomomo/article/details/7897943 "quiesce" 说的也不太明确,我的猜测是:比如系统中有一个大功 ...

  3. testng依赖,顺序,跳过

    依赖测试@test(dependsOnMethods = {"open"})@testpublic static void open{    System.out.println( ...

  4. linux atom 不支持中文

    linux atom 不支持中文 1.首先在ubuntu下安装泉驿正黑字体 sudo apt-get install ttf-wqy-* 2.Edit > Preferences > Se ...

  5. 任意阶幻方(魔方矩阵)C语言实现

    魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说夏禹治水时,河南洛阳附近的大河里浮出了一只乌龟,背上有一个很奇怪的图形,古人认为是一种祥瑞,预示着洪水将被夏禹王彻底制服.后人称之为&quo ...

  6. 《BI项目笔记》历年感官评吸质量均值变化分析Cube的建立

    分析主题主要维度:烟叶级别.烟叶级别按等级信息.烟叶级别按分级标准(标准维度)产地(父子维度)检测时间(时间维度,以Tqc_Raw_SmokingTest .CheckTime字段派生CheckDat ...

  7. C语言细节——献给入门者(一)

    C语言细节——献给入门者(一) 主题  输入输出需要注意的细节 首先我们要知道大致有scanf(),printf(),getchar(),putchar(),gets(),puts()这几种输入方式. ...

  8. FileStream和StreamWriter配合写数据流时,出现数据写不进去的问题

    今天做一个数据导进txt小程序时,发现一个问题,就是没有关闭sw的流,只关闭了fs的,结果写不进去数据,看代码是对的,就是不行,最后就随手填上了sw的关闭后,可以了,反复测试,竟然就是这的问题,有点搞 ...

  9. scala中的下划线_

    1.作为“通配符”,类似Java中的*. 例如 import scala.math._ 2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理! 例如 val s = sum(1 to 5 ...

  10. 转json using指令

    using Newtonsoft.Json;using Newtonsoft.Json.Converters; string result = JsonConvert.SerializeObject( ...