[LC] 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
/ \
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的更多相关文章
- 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. 将二 ...
- 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 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 【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 ...
- 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 ...
- [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 ...
- 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]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- 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 ...
随机推荐
- linux centos 7 防火墙相关
centos 7 系统 默认是开启防火墙,而且没有打开80和8080等端口. 因此,今天配置tomcat和nginx后,分别无法正常访问 访问80和8080端口都报:502错误.(错误的网关)查询资料 ...
- js 关联数组
踩得坑: JS ,通过 new Array()创建了一个数组: var param = new Array();param["key1"] = value1;param[&quo ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 运算符
要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下几种运算符: 算术运算符 比较运算符 逻辑运算符 位运算符 算术运算符 MySQL 支持的算术运算符包括: 在除法运算和模运算中, ...
- Python-查找并保存特定字符串后面的字符串
-- -- 本算法用于查找并存储“特定字符串”后面的字符串. -- 举例: strli = "kaka is li is da is wei !" #用于查找的字符串 sep_li ...
- python logging的输出
---恢复内容开始--- python中logging的使用 什么是日志: 日志是一种可以追踪某些软件运行时所发生事件的方法 软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情 ...
- usr/sbin/inetd
root 4 0.0 1344 1204? S 17:09 0:10 /usr/sbin/inetd 运行 Internet 超级 服务器,它负责监听 Internet sockets 上的连接,并调 ...
- Qt5学习笔记(1)-环境配置(win+64bit+VS2013)
Qt5学习笔记(1)-环境配置 工欲善其事必先-不装-所以装软件 久不露面,赶紧打下酱油. 下载 地址:http://download.qt.io/ 这个小网页就可以下载到跟Qt有关的几乎所有大部分东 ...
- 配置Action
配置Action 实现了Action类后,就可以在struts.xml中配置该Action类了.配置Action就是让Struts2 知道哪个Action处理哪个请求,也就是完成用户请求和Action ...
- Android Studio 停靠模式(Docked Mode)
如果之前选了任务一种模式,先全都取消了 然后点击Window -->Active Tool Window-->这个时候就可以选择Docked Mode了
- 内存管理-ARC
推荐文章:http://blog.csdn.net/primer_programer/article/details/14442899 strong:强指针,指向对象,会持有对象,只有当对象无stro ...