Given a binary tree, flatten it to a linked list in-place.

For example, given the following tree:

    1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:

1
\
2
\
3
\
4
\
5
\
6

题目

将左子树所形成的链表插入到root和root->right之间

思路

      1
/
2(root) 假设当前root为2
/ \
3(p) 4
      1
/
2(root)
/ \
3(p)—— 4(root.right) p.right = root.right
     1
/
2(root)
/ \
3(root.right)- 4 root.right = root.left
      1
/
2(root)
\
3(root.right)- 4 root.left - null

代码

 class Solution {
public void flatten(TreeNode root) {
if (root == null) return; // 终止条件
// recursion
flatten(root.left);
flatten(root.right); if (root.left == null) return; // 三方合并,将左子树所形成的链表插入到root和root->right之间
TreeNode p = root.left;
while(p.right != null) {
p = p.right; //寻找左链表最后一个节点
}
p.right = root.right;
root.right = root.left;
root.left = null;
}
}

[leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表的更多相关文章

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

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

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

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

  4. [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表

    /* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...

  5. leetcode 114 Flatten Binary Tree to Linked List ----- java

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

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

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

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

  9. 114. Flatten Binary Tree to Linked List 把二叉树变成链表

    [抄题]: Given a binary tree, flatten it to a linked list in-place. For example, given the following tr ...

随机推荐

  1. 固定顶部指定div不滑动

    .fixed_div { position:fixed; z-index:100; top: 45px; width:100%; height:45px; } 指定div设置属性position:fi ...

  2. 一套海量在线用户的移动端IM架构设计实践分享(含详细图文)(转)

    1.写在前面 1.1.引言 如果在没有太多经验可借鉴的情况下,要设计一套完整可用的移动端IM架构,难度是相当大的.原因在于,IM系统(尤其是移动端IM系统)是多种技术和领域知识的横向应用综合体:网络编 ...

  3. Linux:回收循环创建的多个线程

    上午我说了循环创建多个线程,由于进程与线程是如此的相似,进程我们知道要回收,那么线程也自然要回收啦.我们接着看控制原语: 线程与共享 线程间共享全局变量! [牢记]:线程默认共享数据段.代码段等地址空 ...

  4. angular的启动原理

    当你用浏览器去访问index.html的时候,浏览器依次做了如下一些事情: 加载html,然后解析成DOM: 加载angular.js脚本:加载完成后自执行,生成全局angular对象,监听DOMCo ...

  5. Java中关键字static的使用与作用

    1.static的意义 static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和 ...

  6. CFDA

    cfda数据抓取 1.网站数据是加密的,需要浏览器进行数据解析 2.网址url有js加密 3.PhantomJS无法解析数据, chrome无法获取数据,所有最终选择用Firefox浏览器 impor ...

  7. 学JS的心路历程-JS支持面向对象?(二)

    昨天讲了面向对象的继承,今天我们来谈谈多态和封装吧! 多态polymorphism 抽象讲法解释,就是使用单一界面操作多种型态的物件 继承父类别,定义与父类别中相同的方法,但实作内容不同,称为复写(o ...

  8. 微服务之间的调用(Ribbon与Feign)

    来源:https://blog.csdn.net/jrn1012/article/details/77837658 使用Eureka作为服务注册中心,在服务启动后,各个微服务会将自己注册到Eureka ...

  9. u-boot 内核 启动参数

    kernel如何得到uboot启动信息: http://blog.sina.com.cn/s/blog_89d9bec60101bzen.html u-boot向linux内核传递启动参数: http ...

  10. linux centos 访问根目录 not accessable

    chmod 777 root 即可 或  chmod a+x root https://blog.csdn.net/CSDN_GYG/article/details/73276310