[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 ...
随机推荐
- 关于localstorage的几个测试
1.如果在同一主域名下,localstorage能访问吗? 测试: 在www\index.php 中写入 localstorage 在www\wex5fortp\ui 目录下能否读到?? 结论:可以得 ...
- 一、javase学习:数据库操作练习
package JDBC_TEST; import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLExcept ...
- PTA Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- .net字符串Gzip压缩和base64string转换:
class Program { static void Main(string[] args) { //要压缩的字符串 string data = "13800138000,验证码:1234 ...
- WebConfig 自定义节点configSections配置信息
WebConfig 自定义节点configSections配置信息 示例: <configuration> <configSections> <!-- For ...
- draw: Could not use program error=0x505
原因:Android的模拟器在ADT中调试运行AVD时速度太慢.也就是说创建的虚拟手机配置太好,电脑带不动. 解决办法:从新创建虚拟手机,是手机配置低一点,具体创建方法如下: 开始先点运行,也就是下图 ...
- C#实现简单的委托异步调用
delegate void textAsy(); static void Main(string[] args) { textAsy t = texts; AsyncCallback callBack ...
- 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)
15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...
- notepad++ 配置Python 调试环境 实用版
一. 安装python 1. 下载python 2.7版本并安装: 2. 在安装到自定义python的时候选择 add python to ptah项:
- Linux常用命令笔记一
笔记1:查看ubuntu是32位还是64位的方法及其版本号 查看ubuntu是64还是32的命令如下: uname -m 如果是i386到i686,那就是32位系统:如果是x86_64 ,那就是64系 ...