[Leetcode][JAVA] 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
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
这个提示反而让人联想到用栈或者递归去做。然而严格意义上这样做使用了额外空间。discuss里面有人提供了很好的非递归算法,似乎是Morris遍历的应用:
记录一个节点cur,起始为根节点。对于这个cur节点,如果有左子树,做两件事:
1. 把左子树的最右节点的right指针指向cur的右子树。
2. cur的right指针指向cur的左子树,cur的left指针指向null.
如果没有左子树就不做这两件事。
这样就算扁平化了一个节点。接着把cur挪到cur的右子树根节点去(原来的左子树根节点),然后继续重复这两件事,直到cur为null.
代码如下:
public void flatten(TreeNode root) {
TreeNode cur = root;
while(cur!=null) {
if(cur.left!=null) {
TreeNode leftRight = cur.left;
while(leftRight.right!=null)
leftRight = leftRight.right;
leftRight.right = cur.right;
cur.right = cur.left;
cur.left = null;
}
cur = cur.right;
}
}
附:使用栈实现的前序遍历做法:
public void flatten(TreeNode root) {
Stack<TreeNode> st = new Stack<TreeNode>();
st.push(root);
if(root==null)
return;
while(!st.isEmpty())
{
TreeNode temp = st.pop();
if(temp.right!=null)
st.push(temp.right);
if(temp.left!=null)
st.push(temp.left);
temp.left=null;
if(!st.isEmpty())
temp.right = st.peek();
else
temp.right = null;
}
}
[Leetcode][JAVA] Flatten Binary Tree to Linked List的更多相关文章
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 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 ...
- leetcode dfs Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...
- 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 ...
- 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 ...
- [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 ...
- [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】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 ...
- [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 ...
随机推荐
- 利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别
--------------------------------------------------低调的分割线-------------------------------------------- ...
- [字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案
今天在eclipse中编写pom.xml文件时,注释中的中文被eclipse识别到错误:Invalid byte 1 of 1-byte UTF-8 sequence,曾多次遇到该问题,问题的根源是: ...
- less预处理的好处,补充关于之前发表的rem单位的运用于计算
我认识的less 优点:优雅,好用,简单,可复用性强, 缺点:less并其实不能为我们减少沉余css代码,还是要靠自己的CSS基础去判断哪些是沉余代码或者是可以合并的代码 之前发表的一篇文章一看就懂得 ...
- Storyboard中segue(转场)使用
源引:http://www.2cto.com/kf/201210/161737.html 一.视图切换类型介绍在storyboard中,segue有几种不同的类型,在iphone和ipad的开发中,s ...
- 我的 C++ style
int g_tennis; // not use as possible int make_world() { ; return size; } ; enum Color { RED, GREEN } ...
- MVC结构
MVC结构是其它三个经典的设计模式的演变:观察者模式(Observer)(Pub/Sub), 策略模式(Strategy)和组合模式(Composite). 来自为知笔记(Wiz)
- KMP匹配算法
先来说一下回溯法匹配字符串: 对于主字符串有一个target_index,以target_index(不动)为起点,匹配字符串pattern的长度+target_index为终点,逐个进行比较,当发现 ...
- .net学习笔记--使用抽象方法实现多态
在使用抽象方法实现多态之前,我们必须知道一些知识点: 1.抽象类不能被实例化: 2.抽象类可以包含非抽象成员,它们可以由其子类继承调用. 我们可以先创建一个Person的抽象类,代码如下: abstr ...
- ABAP POPUP函数
POPUP_TO_CONFIRM_LOSS_OF_DATA 弹出一个对话框告知用户有可能丢失数据,询问是否操作继续.POPUP_TO_CONFIRM_STEP 弹出一个对话框询问用户是否操作继续. P ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...