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

这道题就是将数变为只有右节点的树。

递归,不是很难。

递归的时候求出左节点的最右孩子即可。

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

leetcode 114 Flatten Binary Tree to Linked List ----- java的更多相关文章

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

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

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

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

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

  5. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

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

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

  8. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

  9. LeetCode 114. Flatten Binary Tree to Linked List 动态演示

    把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...

随机推荐

  1. 结对编程——关于Fault、Error、Failure程序设计

    一.问题描述:         构造程序,分别是:         •不能触发Fault         •触发Fault,但是不能触发Error         •触发Error,但是不能产生Fai ...

  2. outlook新邮件到达提醒设置以及outlook最小化到托盘设置

    有些邮件是需要马上处理的,因此希望能在收到邮件之后马上就知道,但是有不希望频繁的去检查有没有.outlook可以帮我们轻松做到新邮件到达提醒. 一 .设置outlook新邮件到达提醒:选项->电 ...

  3. iOS中实现多线程的技术方案

    pthread 实现多线程操作 代码实现: void * run(void *param) {    for (NSInteger i = 0; i < 1000; i++) {         ...

  4. 升级或安装 GNOME Shell

    1.安装经典Gnome桌面系统 install gnome-session-fallbackinstall gnome-appletsinstall indicator-applet indicato ...

  5. iphone获取当前磁盘信息

    获取iphone磁盘总大小.已使用空间.空闲空间 [代码]悦德财富:https://www.yuedecaifu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  6. Actioncontext之类的map嵌套,取值

    假设图中最顶端的map设为Actioncontext的map,这种情况,用<s:property value=""/>或者EL表达式取值,可以用#key1.key2.k ...

  7. 关押罪犯(noip2010)

    解法: (1)搜索(30分) (2)二分(此题属于最大值最小问题) (3)贪心+并查集 下面着重说一下“贪心+并查集” 因为有A.B两座监狱,每个犯人不是在A,就是在B监狱. 至于每个犯人在那个监狱, ...

  8. 《hanoi(汉诺塔)问题》求解

    //Hanoi(汉诺)塔问题.这是一个古典的数学问题,用递归方法求解.问题如下: /* 古代有一个梵塔,塔内有3个座A,B,C,开始时A座上有64个盘子,盘子大小不等,大的在下,小的在上. 有一个老和 ...

  9. (转)面向移动设备的HTML5开发框架

    (原)http://www.cnblogs.com/itech/archive/2013/07/27/3220352.html 面向移动设备的HTML5开发框架   转自:http://blogrea ...

  10. pyMagic:用python控制的Geek入门神器

    大学四年快要结束了,这几年也学习了一点新的姿势.最近一直在跟国外的micropython项目,这个项目是由剑桥大学的理论物理学家(theoretical physicist)Damien George ...