随笔一记,留做重温!

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. WPF事件,路由事件

    直接事件模型或CLR事件模型 1事件拥有者 2事件响应者 3事件订阅关系 例如 Window窗口中的控件Button 事件:拥有者Button 事件:Button.Click 事件响应者:Window ...

  2. oracle习题SQL语句练习

    表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Varchar2(3) 否 学号(主码) Sname Varchar2(8) 否 学生姓名 Ssex Varchar2( ...

  3. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  4. TCP/IP的网际层协议——ICMP

    ICMP经常被认为是IP层的一个组成部分.它携带于IP数据包中,ICMP封装在IP数据包内部: IP首部 ICMP数据包 下面是一份差错报文的例子: 最右边的+或者-代表该报文是查询报文还是错误报文. ...

  5. html mysql special character

    function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str. ...

  6. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  7. js函数绑定同时,如何保留代码执行环境?

    经常写js的程序员一定不会对下面这段代码感到陌生. var EventUtil = { addHandler : function(element, type, handler){ if(elemen ...

  8. .net mvc笔记3_Understanding Razor Syntax

    Understanding Razor Syntax MVC3新视图引擎的名字叫做Razor.ASP.NET视图引擎处理web页面,寻找包含在服务器端指令的特殊元素.正如我们前面已经提到的,标准的AS ...

  9. MySQL show binglog event in 'log_name'

    二进制日志文件记录的内容:记录表的更改. 二进制日志文件记录的形式:基于语句的复制.基于行的复制. 两种记录形式的优点与不足: 基于语句的复制-->它不能保证复制的正确性.如随机函数可能在两台机 ...

  10. QtSoap开发web services客户端程序

        首先需要下载QtSoap开源包,下载地址为: http://www.filestube.com/q/qtsoap+download, 我使用的是:qtsoap-2.6-opensource(不 ...