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

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

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

随机推荐

  1. [题解]codevs1001 舒适的路线

    h3 { font-family: Consolas; color: #339966 } .math { font-family: Consolas; color: gray } 题目描述 Descr ...

  2. 阅读笔记Multi-task Learning for Stock Selection [NIPS1996]

    Multi-task Learning for Stock Selection  Joumana Ghosn and Yoshua Bengio 摘要 用人工神经网络预测未来回报以便于做出对应的金融决 ...

  3. url 处理

    一.jsp异步请求后台(servlet) 的url RegisterServlet  与 web.xml 的路径一样 function checkPhoneNumber(){ var phonenum ...

  4. MD5加密字符串

    public static String md5(String string) { byte[] hash; try { hash = MessageDigest.getInstance(" ...

  5. linux操作系统-脚本入门

    背景:在使用linux时,经常会写一些linux命令片段,比较麻烦,有经验的程序员会把 这些碎片式的命令写成shell脚本 1.重启tomcat脚本 #!/bin/sh #kill tomcat pi ...

  6. oracle 第一章总结

    sysdba:  即数据库管理员,权限包括:打开数据库服务器.关闭数据库服务器.备份数据库.恢复数据库.日志归档.会话限制.管理功能.创建数据库.sys用户必须用 sysdba身份才能登录,syste ...

  7. Linux系统下的Nginx安装

    nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好. ububtu平台编译环 ...

  8. ue4 shooterGame 第一步 搭建git linux服务器

    1.分别在linux(服务器)上安装git.和openssh服务, 在windows(客户机)上安装cygwin,模拟linux环境以及安装windows git客户端. 2.windows的cygw ...

  9. 在CMMI推广过程中EPG常犯的错误(转)

    本文转自: http://developer.51cto.com/art/200807/86953.htm 仅用于个人收藏,学习.如有转载,请联系原作者. ---------------------- ...

  10. My安卓知识2--使用listview绑定sqlite中的数据

    我想在我的安卓项目中实现一个这样的功能,读取sqlite数据库中的数据并显示到某个页面的listview控件中. 首先,我建立了一个Service类,来实现对数据库的各种操作,然后在这个类中添加对数据 ...