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】Flatten Binary Tree to Linked List

    随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...

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

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

  4. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

  5. Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)

    114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...

  6. [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

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

  8. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

  9. [LeetCode] 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 ...

随机推荐

  1. iOS 为键盘添加隐藏按钮

    // 为键盘添加隐藏按钮 UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(, , , )]; [backView s ...

  2. ubuntu执行sudo apt-get update提示缺少公钥

    提示信息如下: 获取:1 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease [3,192 B] 命中:2 http:/ ...

  3. ARPSpoofing教程(四) - 捕获并分析数据包

    本程序的主要目标是展示如何解析所捕获的数据包的协议首部. 这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息. 1: #include"pcap.h" 2: ty ...

  4. R包介绍

    R语言的使用,很大程度上是借助各种各样的R包的辅助,从某种程度上讲,R包就是针对于R的插件,不同的插件满足不同的需求,截至2013年3月6日,CRAN已经收录了各类包4338个. 一. R语言包的安装 ...

  5. WebStorm9

    下载地址: http://www.onlinedown.net/soft/554406.htm 注册码: UserName:William===== LICENSE BEGIN =====45550- ...

  6. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  7. dalvik虚拟内存管理之二——垃圾收集

    转载自http://www.miui.com/thread-75028-1-1.html 垃圾收集是dalvik虚拟机内存管理的核心,垃圾收集的性能在很大程度上影响了一个Java程序内存使用的效率.顾 ...

  8. 如何在Asp.Net WebApi接口中,验证请求参数中是否携带token标识!

    [BasicAuthentication] public abstract class ApiControllerBase : ApiController { #region Gloal Proper ...

  9. 第一篇、CSS3_transtion的使用

    <html> <head> <title>这是一个CSS3的特性</title> <style> #box{ width: 150px; h ...

  10. .net软件工程师面试题(参考答案)

    一.填空题(每空1分,共12分) 1面向对象的语言具有__封装______性.__继承_______性.__多态______性. 2能用foreach遍历访问的对象需要实现 ____Ienumerab ...