随笔一记,留做重温!

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

第一个想法是先序遍历,然后按照访问顺序,添加右结点。
public static void flatten(TreeNode root) {
if(root==null){
return ;
}
TreeNode temp=root;
Queue<TreeNode>queue=new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode topNode=queue.poll();
if(topNode.left!=null){
queue.add(topNode.left);
}
if(topNode.right!=null){
queue.add(topNode.right);
}
topNode.left=null;
topNode.right=null; temp.right=topNode;
temp.left=null;
temp=temp.right; }
}
 

结果空间复杂度太高。然后我们参照网上给出的一种思路。

将树拆开。root,root.left(左子树),root.right(右子树)3部分,然后将右子树接在左子树的最右结点(右指针)上。同时,使得root的right指向root.left

root.left=null

root=root.right(下一个过程,循环)

public static void flatten2(TreeNode root) {
if(root==null){
return ;
}
while(root!=null){
TreeNode leftTreeNode=root.left;
TreeNode rightTreeNode=root.right;
if(leftTreeNode!=null){
TreeNode rightmosTreeNode=leftTreeNode;
while(rightmosTreeNode.right!=null){
rightmosTreeNode=rightmosTreeNode.right;
}
rightmosTreeNode.right=rightTreeNode;
root.right=leftTreeNode;
}
root.left=null;
root=root.right;
} }

【LeetCode】Flatten Binary Tree to Linked List的更多相关文章

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

  2. 【Leetcode】【Medium】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 ...

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

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

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

  6. leetcode dfs Flatten Binary Tree to Linked List

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

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

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

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

随机推荐

  1. C/C++中volatile关键字详解 (转)

    1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...

  2. 新浪sae 项目之 git 配置

    新浪sae 项目现在支持git 配置了,但是有好多人配置不成功.下面对这个问题进行一个总结. 1. 在新浪云上面新建项目(该步骤省略) 2. 一般新建完毕后,会让你选择代码的管理工具,如下 注意这里, ...

  3. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. RedHat升级内核成功

    升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...

  5. VS2015如何另存解决方案文件-修改解决方案sln文件的路径

    原文:VS2005如何另存解决方案文件-修改解决方案sln文件的路径 修改解决方案sln文件的路径 方法一:工具→选项→项目和解决方案,可设置项目的默认保存位置.方法二:"解决方案资源管理器 ...

  6. ios post空文件流导致400错误

  7. MyEclipse启动时报 Unable to acquire application service. Ensure that the org.eclips

    今天MyEclipse启动时报如下错误: !SESSION 2012-02-12 11:32:55.198 ---------------------------------------------- ...

  8. [转载]HDFS的'Block'和MapReduce的'Split'之间的关系和区别

    http://www.cnblogs.com/xuxm2007/archive/2011/09/01/2162011.html hadoop的分块有两部分,其中第一部分更为人熟知一点.   第一部分就 ...

  9. data pump(数据泵)

    先给出oracle给出的一个定义: “Oracle Data Pump technology enables very high-speed movement of data and metadata ...

  10. [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175 昨天收到了电子工业出版社寄过来的<cocos2d-x游戏开发之 ...