当前位置:网站首页>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;
}
}
}
边栏推荐
- Codeforces Round #802 (Div. 2)(A-D)
- vscode config
- How does P2P technology reduce the bandwidth of live video by 75%?
- Fedora alicloud source
- How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
- 【上采样方式-OpenCV插值】
- Serious hazard warning! Log4j execution vulnerability is exposed!
- cartographer_local_trajectory_builder_2d
- Introduction to GUI programming to game practice (I)
- How to make your big file upload stable and fast?
猜你喜欢
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)

How to rewrite a pseudo static URL created by zenpart

Schematic diagram of UWB ultra high precision positioning system

FastAdmin Apache下设置伪静态

Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing

瀚高数据库自定义操作符‘!~~‘

基于SDN的DDoS攻击缓解
![[unity3d] rigid body component](/img/57/344aae65e4ac6a7d44b235584f95d1.png)
[unity3d] rigid body component

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
A beginner's entry is enough: develop mobile IM from zero
随机推荐
C# 40. Byte[] to hexadecimal string
Yunqi lab recommends experience scenarios this week, free cloud learning
[red team] what preparations should be made to join the red team?
zencart新建的URL怎么重写伪静态
ssh连win10报错:Permission denied (publickey,keyboard-interactive).
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
Two step processing of string regular matching to get JSON list
PHP二维/多维数组按照指定的键值来进行升序和降序
6.1 - 6.2 introduction to public key cryptography
Anaconda creates tensorflow environment
【Unity3D】碰撞体组件Collider
【红队】要想加入红队,需要做好哪些准备?
Fedora alicloud source
Status of processes and communication between processes
Codeforces Round #800 (Div. 2)
Technical problems to be faced in mobile terminal im development
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
[latex] error type summary (hold the change)
Serious hazard warning! Log4j execution vulnerability is exposed!