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

  1. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

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

  3. [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 ...

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

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

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

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

  8. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

  9. LeetCode 114. Flatten Binary Tree to Linked List 动态演示

    把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...

随机推荐

  1. Esper学习之九:EPL语法(五)

    本篇的内容主要包括了Subquery(也就是子查询)和Join,内容不少,但是不难,基本上和sql差不太多. 1.Subquery EPL里的Subquery和sql的类似,是否比sql的用法更多我不 ...

  2. 【Eclipse】一个简单的 RCP 应用 —— 显示Eclipse 的启动时间。

    1 创建一个插件项目 1.1 File - New - Plug-in Project 注: 1 如果 New 下没有 Plug-in Project , 到 Other 里面去找. 2 如上截图的下 ...

  3. C#串口介绍以及简单串口通信程序设计实现

    C#串口介绍以及简单串口通信程序设计实现 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍 串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口) ...

  4. java(7)LinkedList源码

    系统环境 JDK1.7 LinkedList的基本结构 :在JDK1.6中LinkedList是双向引用的环形结构,JDK1.6中是双向引用的线性结构 提醒:看链表代码时最好用笔画下链表结构 有助于理 ...

  5. 题目1198:a+b(高精度计算,好像有点问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1198 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. 检查mono兼容性的工具MOAM

    mono的迁移工具,可以帮助我们从windows平台迁移到Linux平台,可以用来检测特定的.net的dll或exe程序对mono的兼容性,并能够给出不兼容的方法 项目地址 MoMA 项目介绍 MoM ...

  7. centos 6.6编译安装nginx

    nginx 安装 安装前必要软件准备 1)安装pcre.gzip 等为了支持rewrite功能,我们需要安装pcre # yum install -y pcre* zlib zlib-devel op ...

  8. springMVC前后台交互

    后台返回json对象: package com.sawshaw.controller; import org.springframework.stereotype.Controller; import ...

  9. imageView 的contentMode问题

    UIViewContentModeScaleToFill : 图片拉伸至填充整个UIImageView(图片可能会变形) UIViewContentModeScaleAspectFit : 按照原来的 ...

  10. Spark2 文件处理和jar包执行

    上传数据文件 mkdir -p data/ml/ hadoop fs -mkdir -p /datafile/wangxiao/ hadoop fs -ls / hadoop fs -put /hom ...