[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 ...
随机推荐
- ORM一对多查询对象
正向查询: 取人民日报出版社出版的所有书籍 方式一: pub_obj = Publish.objects.filter(name='人民日报')[0] ret = Book.objects.filte ...
- 容器viewController添加或者删除子viewController
假设有一个viewControllerA,我们想在viewControllerA中添加viewControllerB,需要执行以下方法: [viewControllerA addChildViewCo ...
- webvtt字幕转srt字幕的python程序(附改名程序)
最近写了两个比较简单的python程序,原有都是由于看公开课感觉比较费劲,一个是下载的视频无用的名字太长,另一个就是下载的vtt字幕播放器不识别,写了一个vtt转换成str字幕格式的文件 vtt to ...
- Dictionary,hashtable, stl:map有什么异同?
相同点:字典和map都是泛型,而hashtable不是泛型. 不同点:三者算法都不相同 Hashtable,看名字能想到,它是采用传统的哈希算法:探测散列算法,而字典则采用的是散列拉链算法,效率较高, ...
- jenkins com.jcraft.jsch.JSchException: Auth cancel
jenkins构建时报如下错误: 首先去系统管理--->系统设置上看看SCP插件中的用户名和密码是否正确
- RabbitMQ系列教程之六:远程过程调用(RPC)(转载)
RabbitMQ系列教程之六:远程过程调用(RPC) 远程过程调用(Remote Proceddure call[RPC]) (本实例都是使用的Net的客户端,使用C#编写) 在第二个教程中,我们学习 ...
- Java泛型类型擦除以及类型擦除带来的问题
目录 1.Java泛型的实现方法:类型擦除 1-2.通过两个例子证明Java类型的类型擦除 2.类型擦除后保留的原始类型 3.类型擦除引起的问题及解决方法 3-1.先检查,再编译以及编译的对象和引用传 ...
- SQL Server 2008读取域帐户信息
参考:http://www.pawlowski.cz/2011/04/querying-active-directory-sql-server-t-sql/ 1.建立 link server . u ...
- 学习shell脚本之前的基础知识(一)(学习记录帖)
记录命令历史:我们敲过的命令,linux会有记录,保存在家目录的.bash_history文件中.(备注:只有用户正常退出当前shell时,当前命令才会保存在.bash_history文件中) “ ...
- wamp添加本地虚拟域名
修改一下文件位置,引入文件 下面是配置文件添加配置指向目录 <VirtualHost *:80> ServerAdmin 979996962@qq.com DocumentRoot &qu ...