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. 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码

    转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...

  2. MySql 性能优化杂记

    前一段时间接触MySql 服务器,关于查询忧化方面整理,优化主要唯绕几个工具函数 : show profiling  , explain ,  索引 , limit 如果上司抱怨服务器查询太慢,这时候 ...

  3. [开发笔记]-FireWorks常用操作快捷键

    一.工具快捷键 指针.选择后方对象[V],[0] 部分选定[A],[1] 选取框.椭圆选取框[M] 套索.多边形套索[L] 裁剪.导出区域[C] 魔术棒[W] 线条工具[N] 钢笔工具[P] 矩形.圆 ...

  4. 操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null

    解决方式: 1.jdbc URL链接为:jdbc.url=jdbc:db2://(ip):50000/(数据库名称):driverType=4;fullyMaterializeLobData=true ...

  5. java udp网络编程

    import java.net.*; /* 通过UDP传输发送文字数据 1.建立socket服务 2.提供数据,并封装到数据包中 3.通过sokect服务的发送功能,将数据包发送出去 4.关闭资源 * ...

  6. Configuration for Python to run on Android

    It's exiting to learn to make app on Android with python. But when I check <Head First Python> ...

  7. 谈谈 Mifare Classic 破解

    2008 年的时候,荷兰恩智浦(NXP)公司开发的 RFID 产品 Mifare Classic 就被破解了,黑历史在这里就不在具体说了,想详细了解可以自己 Google 百度.现在还是重点说说关于 ...

  8. 通过AssetsLibrary框架访问所有相片

    该框架下有几个类,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation. ALAssetsLibrary ...

  9. IOS 多线程编程之Grand Central Dispatch(GCD)介绍和使用 多线程基础和练习

    介绍:前面内容源自网络 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式 ...

  10. 在Web.config中注册自定义控件

    之前都是在每个页面的顶端注册用户控件或者是自定义控件,这样不简洁,而且麻烦. 现在只要在在web.config文件中声明,其他地方就可以直接使用前缀加控件如下黄色代码所示: <configura ...