【114-Flatten Binary Tree to Linked List(二叉树转单链表)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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

题目大意

  给定一棵二叉树。将它转成单链表,使用原地算法。

解题思路

  从根结点(root)找左子树(l)的最右子结点(x)。将root的右子树(r)接到x的右子树上(x的右子树为空)。root的左子树总体调整为右子树,root的左子树赋空。

代码实现

树结点类

public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

算法实现类

public class Solution {

    public void flatten(TreeNode root) {
TreeNode head = new TreeNode(-1);
head.right = root;
TreeNode node = head; while (node.right != null) {
node = node.right;
if (node.left != null) {
TreeNode end = node.left;
while (end.right != null) {
end = end.right;
} TreeNode tmp = node.right; node.right = node.left;
node.left = null;
end.right = tmp;
}
} head.right = null; // 去掉引用方便垃圾回收
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47438085

【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】的更多相关文章

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

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

  2. 【LeetCode从零单排】No 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 ...

  3. 114 Flatten Binary Tree to Linked List 二叉树转换链表

    给定一个二叉树,使用原地算法将它 “压扁” 成链表.示例:给出:         1        / \       2   5      / \   \     3   4   6压扁后变成如下: ...

  4. Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...

  5. 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. 将二 ...

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

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

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

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

  9. leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  10. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

随机推荐

  1. Best Time to Buy and Sell Stock with Cooldown -- LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. db2字符串截取方法及常用函数

    select substr(index_code, 1, locate('-', index_code)-1) from report_data substr(str,m,n)表示从str中的m个字符 ...

  3. 一致性hash-java实现treemap版

    把不同号段的数据储存在不同的机器上,以用来分散压力.假如我们有一百万个QQ号,十台机器,,如何划分呢? 最简单粗暴的方法是用QQ号直接对10求余,结果为0-9 分别对应上面的十台机器.比如QQ号为 2 ...

  4. 【Linux】CentOS7 添加常用源

    CentOS 的官方源去掉了一些与版权有关的软件,因此想要安装这些软件或者手动下载安装,或者使用其他源. 下面是添加EPEL源和RPMforge源的步骤. 1.首先, 添加源之前要确定系统架构及版本 ...

  5. 彻底解决DZ大附件上传问题

    个. 注意:很多人遇到修改php.ini后重应WEB服务后仍然不能生效.这种情况应该先确认一下所改的php.ini是不是当前PHP所使用的.您可以在WEB目录下建立一个php文件,内容很简单就一句话& ...

  6. Delphi之过程与函数

    过程以保留字procedure开始,没有返回值:函数以保留字function开始,有返回值. 参数位于括号里面,多个参数之间以分号分隔,例如: procedure SetDate(Year: Inte ...

  7. org.apache.hadoop.ipc.RemoteException: User: root is not allowed to impersonate root

    修改 core-site.xml文件 vim /usr/local/hadoop/etc/hadoop/core-site.xml 增加: <property> <name>h ...

  8. 使navicat可以通过SSH连接MySQL数据库

    1.编辑/etc/ssh/sshd_config,在最下面添加如下语句 KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libss ...

  9. EffectiveJava(21)策略模式多种方式实现字符串比较

    **调用对象上的方法通常是执行该对象上的某项操作**. 如果一个对象的方法执行其他对象的操作,一个类仅仅导出这个方法(它的实例相当于一个指向该方法的指针),这样的实例被称为函数对象 例如: /** * ...

  10. mysql binlog 使用

    用于数据恢复的binlog 前提条件 1.定时mysqldumps全备数据库 2.开启binlog增量备份 情景:手滑误操作删表操作 立刻 mysql>flush logs;  #开启一个新的b ...