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 Solution 1:
 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: None Do not return anything, modify root in-place instead.
"""
self.prev = None
def dfs(root):
if root is None:
return None
# reverse preOrder traveral
# use pre_node to track the previous node to connect as current right
dfs(root.right)
dfs(root.left)
root.right = self.prev
root.left = None
self.prev = root
dfs(root)

Solution 2:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
helper(root);
} private TreeNode helper(TreeNode root) {
if (root == null) {
return null;
} TreeNode lastLeft = helper(root.left);
TreeNode lastRight = helper(root.right);
if (lastLeft != null) {
lastLeft.right = root.right;
root.right = root.left;
root.left = null;
}
if (lastRight != null) {
return lastRight;
}
if (lastLeft != null) {
return lastLeft;
}
return root;
}
}

Solution 3:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
stack.offerFirst(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pollFirst();
if (cur.right != null) {
stack.offerFirst(cur.right);
}
if (cur.left != null) {
stack.offerFirst(cur.left);
}
if (!stack.isEmpty()) {
cur.right = stack.peekFirst();
}
cur.left = null;
}
}
}

[LC] 114. Flatten Binary Tree to Linked List的更多相关文章

  1. 114 Flatten Binary Tree to Linked List [Python]

    114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...

  2. 114. Flatten Binary Tree to Linked List(M)

    . Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...

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

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

  4. 【LeetCode】114. Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...

  5. 114. Flatten Binary Tree to Linked List 把二叉树变成链表

    [抄题]: Given a binary tree, flatten it to a linked list in-place. For example, given the following tr ...

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

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

  8. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

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

随机推荐

  1. Android如何用一个TextView显示不同颜色得字符

    最近做一个项目,需要一个字符串显示不同的颜色.当时直接想到的就是用多个TextView来拼接,但是如果字符数量多的话,这样写是非常麻烦得.而且还要增加很多控件. 后来发现一个非常方便得方法.直接看代码 ...

  2. NetWork--HTTPS 原理解析<转>

    转载链接:https://www.cnblogs.com/zery/p/5164795.html HTTPS 原理解析   一 前言 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网 ...

  3. pycharm调试、设置汇总

    目录: 1.pycharm中不能run 2.pycharm基本调试操作 3.pycharm使用技巧 4.pycharm Error running draft: Cannot run program ...

  4. opencv matchTemplate函数用法

    模板匹配函数,就是在一幅图中,找到另外一幅的在本图的相似的地方 CV_EXPORTS_W void matchTemplate( InputArray image, InputArray templ, ...

  5. Python笔记_第四篇_高阶编程_高阶函数_3.sorted

    1. sorted函数: 常用的排序分:冒泡排序.选择排序.快速排序.插入排序.计数器排序 实例1:普通排序 # 普通排序 list1 = [,,,,] list2 = sorted(list1) # ...

  6. 打水滴(BFS)

    在一个n行m列的网格中,某些位置存在一些水滴.嘟嘟进行q次打水滴操作,每次嘟嘟在某一个网格当中添加一个水滴,当某一网格中的水滴数量超过L时,该网格中的水滴变为四个水滴,并分别向上下左右四个方向飞出,每 ...

  7. 2018.11.2JavaScript随笔

    构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型

  8. C#判断两个字符串是否相等的方法 ,还有char赋空值办法。

    string str1="Test"; string str2 = "Test"; if (str1==str2) //第一种判断方式 { //第二种判断方式 ...

  9. 初学C#之运算符和关系表达式

    ㈠运算符和关系表达式 一元运算符++.-- 前加和后加区别,事例++在后如下: ; ; //结果age=19 说明age++在表达式中age的值也+1.结果sum=8,原因age++,++在后用age ...

  10. Java之多线程方式一(继承Thread类)

    /** * 多线程的创建,方式一:继承于Thread类 * 1. 创建一个继承于Thread类的子类 * 2. 重写Thread类的run() --> 将此线程执行的操作声明在run()中 * ...