【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. Git&GitHun 命令合集

    Git&GitHun 命令合集 基本操作 git --version 查看git版本信息 git add 本地库初始化 设置签名 git config user.name xxx git co ...

  2. 状压DP【p1896】[SCOI2005]互不侵犯

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  3. 树的直径【bzoj3363】 [Usaco2004 Feb]Cow Marathon 奶牛马拉松

    Description 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个最远农场间的 ...

  4. Quaternion Euler

    geometry_msgs::Quaternion orientation = map->info.origin.orientation;      tf::Matrix3x3 mat(tf:: ...

  5. 洛谷 P3636 曲面

    题目背景 xht喜欢研究数学函数,他特别喜欢反比例函数. 题目描述 我们知道,反比例函数xy=a的图象是双曲线. xht于是想:把它推广到三维是什么样的呢? 定义曲面C(k)为方程xyz=k所确定的曲 ...

  6. POJ3345 Bribing FIPA(树形DP)

    题意:有n个国家,贿赂它们都需要一定的代价,一个国家被贿赂了从属这个国家的国家也相当于被贿赂了,问贿赂至少k个国家的最少代价. 这些国家的从属关系形成一个森林,加个超级根连接,就是一棵树了,考虑用DP ...

  7. Delphi TClientDataset查找定位功能

    if CDSUserFunc.Locate('mod_id;res_id', VarArrayOf([UserFunc.MOD_ID, UserFunc.RES_ID]), [loCaseInsens ...

  8. iOS用户响应者链的那些事儿

    这篇文章想跟大家分享的主旨是iOS捕获用户事件的各种情况,以及内部封装的一些特殊事件. 我们先从UIButton谈起,UIButton大家使用的太多了,他特殊的地方就在于其内置的普通Default/高 ...

  9. [Android Traffic] 让android应用在传输网络数据的时候更省电

    到今年6月,我国的手机网民已经达到了3.88亿,超过了电脑终端.相信有智能机的同学都用过手机上网冲浪.但是手机的电量很快被用光了恐怕是每个人都不能忍受的一件事情.而打开数据连接进行网络数据的传输是很耗 ...

  10. ElasticSearch5.5.2常用命令

    1.启动 转到elasticsearch-5.5.2\bin目录: 打开命令行输入:elasticsearch 2.ELasticsearch集群已经启动并且正常运行 curl http://127. ...