[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 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将二叉树展成一个链表的更多相关文章
- [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 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- [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 ...
- [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- ubuntu卸载福昕阅读器
在安装目录找到maintenancetool.sh运行之 ~/opt/foxitsoftware/foxitreader
- Error building Player: Exception: Could not start java
4.6.1发布Flash版本出错.解决方法:把C:\Windows\System32\java.exe复制到C:\Windows\SysWOW64下即可
- [PHP]对Json字符串解码返回NULL的一般解决方案
---------------------------------------------------------------------------------------------------- ...
- [Linux]Ubuntu下安装Sublime-text 且 支持中文输入
------------------------------------------------------------------------------------------ 首先进行如下操作: ...
- 【转】Classful IPv4 addressing definition
Classful addressing definition Class Leadingbits Size of networknumber bit field Size of restbit fie ...
- MVC part4
SpringMVC 注解 @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写,你也可以自己指定, 如下 方法一: @Controller ...
- Eclipse里的代码光标变成一个黑色块
以前经常在编写程序是不知到碰到键盘上的那个键了,或是那几个组合键了,使得Eclipse里的代码光标变成一个黑色块:在这个状态下,光标不在活动自如,只能一直往后写代码,就不想平时的 " | & ...
- linux安装node简单方法
1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过 uname -a ...
- K-means算法的实现
K-MEANS算法是一种经典的聚类算法,在模式识别得到了广泛的应用.算法中有两个关键问题需要考虑:一是如何评价对象的相似性,通常用距离来度量,距离越近越相似:另外一个是如何评价聚类的效果,通常采用误差 ...
- sqlite 时间戳转时间
), 'unixepoch','localtime') from messages where data != '' order by timestamp desc 官方eg: Examples Co ...