随笔一记,留做重温!

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. Clamp函数

    Clamp函数可以将随机变化的数值限制在一个给定的区间[min, max]内: template<class T> T Clamp(T x, T min, T max) { if (x & ...

  2. JavaEE学习之设计模式

    转自:http://mp.weixin.qq.com/s?__biz=MjM5OTMxMzA4NQ==&mid=221913387&idx=2&sn=d5d006300722f ...

  3. Spring总结——控制反转,注入(配置和注解两种方式)

    一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...

  4. Mybatis使用存储过程(MySql)

    推荐文章:http://www.iteye.com/topic/1132302 http://yhjhappy234.blog.163.com/blog/static/3163283220124557 ...

  5. 页面打开直接执行a点击事件

    <script> window.onload = function(){ function autoclick(){ var url = document.getElementById(' ...

  6. 安卓自定义view_GDI绘图 _2d绘图_canvas绘图

    2014年到2016年 发生了很多事情,如今已成定局,现在想忘掉这些烦恼的事情,找点以前想干没有干的事情来做,塞满大脑就不去想了. 之前,一直想做一款挂机类游戏,各种平台和开发语言都选择过了,从htm ...

  7. VS中,NUnit适合测试者尽心开发自动化测试,而Unit适合开发者开发单元测试。

    1.整合Visual Studio和NUnit 在Visual Studio 2010中,通过安装NUnit插件,可以不使用外部客户端,直接运行测试. 当然,貌似在最新版本的VS2012中,安装过NU ...

  8. docker10件事

    docker – 你应该知道的10件事   容器并不是一个全新的技术,但这并不妨碍Docker如风暴一样席卷整个世界. 如果你在IT圈里,你一定听说过Docker.就算与其他热门技术,如:Puppet ...

  9. linux 6.4平台利用rman迁移oracle 11g r2数据库

    测试环境分别在虚拟机安装A,B主机 系统:linux 6.4, 数据库:oracle 11g r2 A主机:安装oracle 11g r2数据库 B主机:只安装oracle 11g r2软件 第一步, ...

  10. 编程实现Windows系统自动登录

    编程实现Windows系统自动登录 原理: 通过注册表修改实现.Windows内置了自动登录的机制,在登录系统时,winlogon会检查注册表下有没有设置自动登录,如果设置了就上就会读取用户名和密码, ...