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
Accepted
218,918
Submissions
533,947

【解析】由上图可知,如果右子树不为空,则右子树最后肯定为左子树最有一个靠右的孩子节点的右子树,而左子树最后成为整棵树的右子树。这样,首先判断左子树是否为空,不为空就寻找到树根的左孩子节点,然后寻找该节点是否有右孩子,如果有继续寻找,直到找到属于叶子节点的右孩子,此时,该节点的右子树“指向”当前树的右子树,并将当前左子树变为树根的右孩子,将整棵树左孩子置为空。最后,根节点“指向”根节点的右孩子,继续上述操作,直到整棵树遍历完即得到结果。

1.首先,将根节点的右子树,连接到根节点的左子树的最右节点,如下图所示:

2.然后将左子树移动到右边,如下图所示:

3.之后,将根节点的右节点 作为 当前的根节点,循环进行。

                              

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

114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】的更多相关文章

  1. 114 Flatten Binary Tree to Linked List [Python]

    114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...

  2. 114. Flatten Binary Tree to Linked List(M)

    . Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...

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

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

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

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

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

  7. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

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

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

随机推荐

  1. nodejs+react构建仿知乎的小Demo

    一.命令行进入指定项目文件夹 二.相关命令安装环境和项目工具 npm init npm install react -- save npm install -g gulp npm install -- ...

  2. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  3. ZooKeeper屏障和队列的指南(七)

    引言 在这个指南中,使用展示了使用ZooKeeper实现的屏障和生产-消费队列.我们分别称这些类为Barrier和Queue.这些例子假定你至少有一个运行的ZooKeeper服务. 两个原语都使用下面 ...

  4. 原生js addclass,hasClass,removeClass,toggleClass的兼容

    (function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...

  5. 【Linux学习】nohup后台运行程序以及输出重定向

    Linux有两种命令使程序后台运行 第一种:支持后台运行,但是关闭终端的话,程序也会停止 command & 第二种:支持后台运行,关闭终端后,程序也会继续运行 nohup command & ...

  6. centos_7.1.1503_src_4

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ libkcompactdisc-4.10.5-3.el7.src.rpm 05-Jul-20 ...

  7. EXT入门学习

    今天,对EXT做了一下初步的了解,了解了一些基本用的函数.窗体对象.表单.文本域.按钮,一些基本的函数我列了出来,写了个登陆的demo,是根据别人的例子模仿出来的,见谅哈. 基本函数 Ext.onRe ...

  8. python初学-元组、集合

    元组: 元组基本和列表一样,区别是 元组的值一旦创建 就不能改变了 tup1=(1,2,3,4,5) print(tup1[2]) ---------------------------------- ...

  9. tomcat - gc日志输出

    原创 2017年01月04日 14:32:37 2090 tomcat/bin catalina.sh JAVA_OPTS='-server -Xms4g -Xmx4g -Xss256k -XX:Pe ...

  10. experss 做小型服务器出现跨域问题

    情况是这样的 我用express做一个小型的服务器来做我demo项目的一个接口 然后我就出现了跨域问题 然后我就 app.all('/*', function(req, res, next) { // ...