[Leetcode][JAVA] Flatten Binary Tree to Linked List

时间:2021-07-06 07:37:16

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
Hints:

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的更多相关文章

  1. LeetCode 114&vert; Flatten Binary Tree to Linked List(二叉树转化成链表)

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

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

  3. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

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

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

  6. &lbrack;LeetCode&rsqb; 114&period; 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 ...

  7. &lbrack;LeetCode&rsqb; 114&period; 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】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 ...

  9. &lbrack;leetcode&rsqb;114&period; 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 ...

随机推荐

  1. java面向对象(封装-继承-多态)

    框架图 理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程强调的是功能行为 面向对象将功能封装进对象,强调具备了功能的对象. 面向对象是基于面向过程的. 面向对象的特点 ...

  2. 重写,重载,super,this,继承

    重写:overwrite/override 子类根据需要对从基类继承来的方法进行重写. 重写方法必须与被重写方法有相同的方法名,参数列表和返回类型. 重写方法不能使用比被重写方法更严格的访问权限. 重 ...

  3. javascript学习(一) 异常处理与简单的事件

    一:异常处理 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></ti ...

  4. python入门笔记第一天

    查询acsii命令 ord(‘A’) 导入模块python执行系统命令显示文件.查找文件方法1import osa = os.popen('目标').read()a 解释output = os.pop ...

  5. UIView 设置阴影&lpar;属性说明&rpar;

    以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowCo ...

  6. android com&period;handmark&period;pulltorefresh 使用技巧

    近期使用android com.handmark.pulltorefresh 遇到一些小问题.如今总结一些: 集体使用教程见: http://blog.csdn.net/harvic880925/ar ...

  7. 应对Gradle联网问题、长时间卡在resolve dependencies的思路

    1.出现这种情况,在首先考虑网络问题,依赖下载不下来尝试*,未果. 2.检查防火墙问题,更换host,无法解决. 3.新建Gradle工程,依然卡在resolve dependen ...

  8. HDU 1872:稳定排序

    稳定排序 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. tomcat 6&period;x &plus; log4j日志配置并按天(或大小)生成文件

      tomcat日志,默认路径在${catalina.home}/logs目录下,默认使用的是tomcat自己封装的logging工具类,默认配置文件使用的${catalina.home}/conf/ ...

  10. ios系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...