[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 ...
随机推荐
- dp--悬线dp P4147 玉蟾宫
题目背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 题目描述 这片土地被分成N*M个格子,每个格子里写着'R'或者'F ...
- nodejs(13)模块加载机制
模块加载机制 优先从缓存中加载 当一个模块初次被 require 的时候,会执行模块中的代码,当第二次加载相同模块的时候,会优先从缓存中查找,看有没有这样的一个模块! 好处:提高模块的加载速度:不需要 ...
- 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard
在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(
- windows更新系统系统时无法更新
win7系统显示“windows update当前无法检查更新,因为未运行服务,您可能需要重新启动计算机” 解决方法 1.win+r打开运行窗口,输入CMD 2.在dos窗口中输入命令“net sto ...
- PAT-输入输出
测试样例输入方式 while...EOF型(题目没有给定输入的结束条件) while(~scanf("%s",s)) {} //等价于while(scanf("%s&qu ...
- Serverless 公司的远程团队沟通策略
本文系译文,Serverless 团队分散在全球各地,本文介绍我们如何管理沟通策略和远程协作. 原作者:FelixDesroches 译者:Aceyclee 首先向不了解我们的人说明一下,Server ...
- 成为优秀Angular开发者所需要学习的19件事
一款to-do app基本等同于前端开发的"Hello world".虽然涵盖了创建应用程序的CRUD方面,但它通常只涉及那些框架或库也能做到的皮毛而已. Angular看起来似乎 ...
- c语言中assert的用法
/************************************************************************* > File Name: assert.c ...
- Disruptor的简单介绍与应用
前言 最近工作比较忙,在工作项目中,看了很多人都自己实现了一套数据任务处理机制,个人感觉有点乱,且也方便他人的后续维护,所以想到了一种数据处理模式,即生产者.缓冲队列.消费者的模式来统一大家的实现逻辑 ...
- 洛谷 P5018 对称二叉树
题目传送门 解题思路: 先计算每个点的子树有多少节点,然后判断每个子树是不是对称的,更新答案. AC代码: #include<iostream> #include<cstdio> ...