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. DJango之视图函数

    一)Django WEB框架 2) request.path和request.get_full_path() 是请求的路径 3)render:页面渲染 4)redirect:页面跳转 3)模板语法: ...

  2. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  3. Delphi TQuery 的Locate用法

    Help里的解释 function Locate(const KeyFields: String; const KeyValues: Variant; Options: TLocateOptions) ...

  4. map和hasmap的区别

    MAP接口的定义如下: public interface MAP< k , v>      Key 到value 的映射 ,Key不允许重复,每一个key只能映射一个value . Has ...

  5. Structs复习 Path问题

    Path问题相对复杂 主要是路劲问题 但结论很简单 就是统一使用绝对路径 jar包 web.xml <?xml version="1.0" encoding="UT ...

  6. c++之enum的好处与 define 的区别

    转载自 https://blog.csdn.net/zhh464626057/article/details/41038933 什么时候需要用到enum呢?就是变量的数值在几个范围之间.red,blu ...

  7. Anaconda安装及配合pycharm使用

    首先到https://www.anaconda.com/download/下载合适的anaconda版本.如Windows 64bit. 下载了直接双击开始下载,一路同意下去,到选择安装的目录.这里选 ...

  8. perl5

    1.perl包加入环境 export PERL5LIB=/export/personal1/wanglh/.software/perl/lib:$PERL5LIB

  9. MySQL 5.7 使用原生JSON类型

    首先回顾一下JSON的语法规则: 数据在键值对中, 数据由逗号分隔, 花括号保存对象, 方括号保存数组. 按照最简单的形式,可以用下面的JSON表示: {"NAME": " ...

  10. Java的map键值对的用法,map的遍历,Entry对象的使用

    思路: 1.定义集合 2.存储数据 3.添加元素 4.遍历 4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象) 4.2声明迭代器或者用for增强循环 4.3 ...