当前位置:网站首页>牛客题目——二叉搜索树与双向链表
牛客题目——二叉搜索树与双向链表
2022-07-27 14:54:00 【zhangzhang_one】
题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表,如下图所示:
要求:空间复杂度为O(1),即不能创建任何新的结点,只能调整树中结点的指向,时间复杂度为O(n)。
示例
输入:{10,6,14,4,8,12,16}(二叉树的根结点)
输出:双向链表的头结点。
二叉搜索树
二叉搜索树,也称二叉排序树,具有以下性质:
- 若左子树不为空,则左子树上的所有结点的值均小于根结点的值;
- 若右子树不为空,则右子树上所有结点的值均大于根结点的值;
- 左右子树也分别为二叉排序树。
由于二叉搜索树具有以上性质,可以快速查找、插入和删除。
解题思路
可以发现,二叉树的中序遍历刚好是排序好的序列,因此我们可以中序遍历二叉树,将遍历结果存储到数组当中,再对数组遍历,改变结点的左右结点,最后生成双向链表,但是其空间复杂度为O(n)。
既然利用数组不满足时间复杂度要求,我们可以通过创建两个指针结点,在遍历的同时调整结点之间的指针,具体做法如下:
- 创建pre结点与head结点;
- 若二叉树为空,则返回空;
- 否则递归遍历左子树;
- 遍历当前结点,并修改当前结点的left以及pre结点的right;
- 更新pre结点为当前结点;
- 若head结点为空,则将head结点指向当前结点;
- 递归遍历当前结点的右子树;
- 返回双向链表的头结点。
代码实现
//Java代码
public class Solution {
TreeNode pre = null;
TreeNode head = null;
public void inOrder(TreeNode node){
if(node == null) return;
inOrder(node.left);
if(pre != null){
this.pre.right = node;
node.left = this.pre;
}
this.pre = node;
if(head == null) this.head = node;
inOrder(node.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
inOrder(pRootOfTree);
return this.head;
}
}
# Python代码
class Solution:
def Convert(self , pRootOfTree ):
def inOrder(node):
if node == None:
return
inOrder(node.left)
if self.pre != None:
self.pre.right = node
node.left = self.pre
if self.head == None:
self.head = node
self.pre = node
inOrder(node.right)
self.pre = None
self.head = None
inOrder(pRootOfTree)
return self.head
边栏推荐
- Jerry's built-in touch parameters for modification [chapter]
- Duplicate numbers in array
- Cvxpy - latest issue
- MQ Series 2: technology selection of Message Oriented Middleware
- LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
- Notes on implementation and acquisition of flowable custom attributes
- 2021-06-02
- Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
- Gurobi——GRBEnv
- Clear understanding of torchvision (mind map)
猜你喜欢

MySQL high version report SQL_ mode=only_ full_ group_ By exception

【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar

Jupyter creates a virtual environment and installs pytorch (GPU)

training on multiple GPUs pytorch

The image displayed online by TP5 is garbled
![Jerry's built-in touch parameters for modification [chapter]](/img/6b/38c3ad28a7256e5e41bb444d0993db.png)
Jerry's built-in touch parameters for modification [chapter]

Is low code the future of development? On low code platform

UML图介绍

知网、万方数据库免费下载论文------比连接学校内网速度快数倍不止(有的学校万方数据库不支持下载)

Scala branch control (single branch, double branch, multi branch), return value of branch judgment
随机推荐
CDQ divide and conquer and whole dichotomy learning notes
什么是jsp?
[paper reading] a CNN transformer hybrid approach for coding visual neuralactivity into text
Insert pictures in word to maintain high DPI method
Jerry's built-in touch parameters for modification [chapter]
The difference between MVC and MVP and MVVM
mvc和mvp和mvvm的区别
Supplement - example of integer programming
京东张政:内容理解在广告场景下的实践和探索
The class declared by the scala object keyword directly calls methods and associated objects
Gurobi——GRBLinExpr
As changes the background theme and background picture
2021-05-30
log4j.jar和slf4-log4下载链接
LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
excel skill
TP5 rewrite paging
除了「加机器」,其实你的微服务还能这样优化
Unable to enter the function definition after transferring the IAR project folder to the directory
Cvxpy - latest issue