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.

原题链接:https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/

题目:给定二叉树,按前序位置展平成一个链表。

思路:递归处理。把右子树放到左子树之后,并清空左子树。

	public void flatten(TreeNode root) {
if(root == null)
return;
flatten(root.left);
flatten(root.right);
TreeNode tmp = root;
if(tmp.left == null)
return;
else
tmp = tmp.left;
while(tmp.right != null)
tmp = tmp.right;
tmp.right = root.right;
root.right = root.left;
root.left = null;
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

相同的做法。可是非递归。

	public void flatten(TreeNode root){
while(root != null){
if(root.left != null){
TreeNode tmp = root.left;
while(tmp.right != null)
tmp = tmp.right;
tmp.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
}

reference : http://blog.csdn.net/perfect8886/article/details/20000083

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——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-place. For ex ...

  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-place. For e ...

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

  4. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

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

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

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

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

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

  9. 【LeetCode】114. 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 ...

随机推荐

  1. Directx11学习笔记【一】 最简单的windows程序HelloWin

    声明:本系列教程代码有部分来自dx11龙书及dx11游戏编程入门两本书,后面不再说明 首先,在vs2013中创建一个空的解决方案Dx11Demo,以后的工程都会放在这个解决方案下面.然后创建一个win ...

  2. 1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup

     1 ip ad查看网卡编号 2.ifconfig查看网卡信息 3.关闭网卡 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcX ...

  3. Java线程学习笔记(一个)

    一个.正在创建的线程: 老掉牙的话题了.继承 java.lang.Thread父类或者实现Runnalbe接口.这里就提一句: class Thread implements Runnable Thr ...

  4. iOS 面试题:OC标题的基本概念<延续>

    第一,如何确定一个方法方法名称 删除减号,加,删除返回值,删除参数类型,删除参数,剩下的就是的方法名 秒,id,能够用assign,copy,retain,依据须要使用 第三,autorelease ...

  5. 第十三章——表和索引分区(1)——使用Range Left进行表分区

    原文:第十三章--表和索引分区(1)--使用Range Left进行表分区 前言: 如果数据表的数据持续增长,并且表中的数据量已经达到数十亿甚至更多,数据的查询和操作将非常困难,面对非常庞大的表,几时 ...

  6. 【转】window.scroll 浏览器滚动条的参数总结

    如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...

  7. HDU2647(拓扑排序+反向建图)

    题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,因为尽可能的让老板少付钱,那么a的工资就是b的工资+1.能够确定关系为a>b,依据拓扑排序建边的原则是把"小于" ...

  8. UVa 10397 Connect the Campus

    最小生成树 Kruskal #include<cmath> #include<iostream> #include<cstdio> #include<algo ...

  9. 世界上速度最快的输入法 Fleksy 为了支持中国

    在 Windows Phone 8.1 内置 Word Flow 宣布取得世界最快输入记录后仅几天就打破的 Fleksy 開始測试中文支持了. 总算有一次创新的输入模式里,中文不是被落下的那个.Fle ...

  10. (DDD)仓储的思考

    (DDD)仓储的思考 为什么需要仓储呢?领域对象(一般是聚合根)的被创建出来后的到最后持久化到数据库都需要跟数据库打交道,这样我们就需要一个类似数据库访问层的东西来管理领域对象.那是不是我们就可以设计 ...