当前位置:网站首页>Leetcode114. 二叉树展开为链表
Leetcode114. 二叉树展开为链表
2022-06-26 05:17:00 【Java全栈研发大联盟】
1、题目描述
给你二叉树的根结点 root ,请你将它展开为一个单链表:
- 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
- 展开后的单链表应该与二叉树 先序遍历 顺序相同。
示例 1:
输入:root = [1,2,5,3,4,null,6]
输出:[1,null,2,null,3,null,4,null,5,null,6]
方案一 前序遍历和展开分开进行
解题思路:
首选我们可以先用一个list把前序遍历的结果存起来。 然后再对list中的元素的right指针进行操作即可。
代码如下
class Solution {
public void flatten(TreeNode root) {
List<TreeNode> list = new ArrayList<TreeNode>();
preorderTraversal(root, list);
int size = list.size();
for (int i = 1; i < size; i++) {
TreeNode prev = list.get(i - 1), curr = list.get(i);
prev.left = null;
prev.right = curr;
}
}
public void preorderTraversal(TreeNode root, List<TreeNode> list) {
if (root != null) {
list.add(root);
preorderTraversal(root.left, list);
preorderTraversal(root.right, list);
}
}
}
方案二
可以发现展开的顺序其实就是二叉树的先序遍历。算法和 94 题中序遍历的 Morris 算法有些神似,我们需要两步完成这道题。
- 将左子树插入到右子树的地方
- 将原来的右子树接到左子树的最右边节点
- 考虑新的右子树的根节点,一直重复上边的过程,直到新的右子树为 null
可以看图理解下这个过程。
1
/ \
2 5
/ \ \
3 4 6
//将 1 的左子树插入到右子树的地方
1
\
2 5
/ \ \
3 4 6
//将原来的右子树接到左子树的最右边节点
1
\
2
/ \
3 4
\
5
\
6
//将 2 的左子树插入到右子树的地方
1
\
2
\
3 4
\
5
\
6
//将原来的右子树接到左子树的最右边节点
1
\
2
\
3
\
4
\
5
\
6
......
代码如下:
public void flatten(TreeNode root) {
while (root != null) {
//左子树为 null,直接考虑下一个节点
if (root.left == null) {
root = root.right;
} else {
// 找左子树最右边的节点
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;
}
}
}
边栏推荐
- Muke.com actual combat course
- Transport layer TCP protocol and UDP protocol
- Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages
- 二次bootloader关于boot28.asm应用的注意事项,28035的
- Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
- RESNET practice in tensorflow
- [latex] error type summary (hold the change)
- First day of deep learning and tensorflow learning
- Why does the mobile IM based on TCP still need to keep the heartbeat alive?
- Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法
猜你喜欢
Anaconda creates tensorflow environment
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
[red team] what preparations should be made to join the red team?
6.1 - 6.2 公鑰密碼學簡介
【上采样方式-OpenCV插值】
PHP二维/多维数组按照指定的键值来进行升序和降序
cartographer_local_trajectory_builder_2d
Experience of reading the road to wealth and freedom
Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
随机推荐
Codeforces Round #800 (Div. 2)
Sentimentin tensorflow_ analysis_ layer
As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
C# 40. byte[]与16进制string互转
[unity3d] rigid body component
Ai+ remote sensing: releasing the value of each pixel
How does P2P technology reduce the bandwidth of live video by 75%?
Pytorch forecast house price
LSTM in tensorflow_ Layers actual combat
Guanghetong and anti international bring 5g R16 powerful performance to the AI edge computing platform based on NVIDIA Jetson Xavier nx
thread priority
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
6.1 - 6.2 introduction to public key cryptography
cartographer_ local_ trajectory_ builder_ 2d
Pycharm package import error without warning
Procedural life
[latex] error type summary (hold the change)
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
Practical cases | getting started and mastering tkinter+pyinstaller