当前位置:网站首页>Niu Ke's questions -- binary search tree and bidirectional linked list
Niu Ke's questions -- binary search tree and bidirectional linked list
2022-06-11 18:28:00 【HHYX.】
Binary search tree and double linked list
Topic link : Binary search tree and double linked list
Title Description
Enter a binary search tree , The binary search tree is transformed into a sorted double linked list . As shown in the figure below 
Data range : Enter the number of nodes of the binary tree 0 \le n \le 10000≤n≤1000, The value of each node in the binary tree 0\le val \le 10000≤val≤1000
requirement : Spatial complexity O(1)O(1)( That is, operate on the original tree ), Time complexity O(n)O(n)
Be careful :
1. It is required that no new nodes can be created , You can only adjust the direction of the node pointer in the tree . When the conversion is complete , The left pointer of the node in the tree needs to point to the precursor , The right pointer of the node in the tree needs to point to the successor
2. Returns the pointer of the first node in the linked list
3. Function return TreeNode, There are left and right pointers , In fact, it can be regarded as a data structure of a two-way linked list
4. You don't have to output a two-way linked list , The program will automatically print out according to your return value
Input description :
The root node of a binary tree
Return value description :
One of the head nodes of the two-way linked list .
Topic analysis
The problem is to change a binary search tree structure into a two-way linked list , The left pointer points to the front drive , Right pointer to successor . At the same time, the two-way linked list is required to be orderly . One of the characteristics of binary search tree is that if you traverse the binary search tree in middle order, you can get an orderly arrangement . You must modify the original tree and cannot create new nodes . So how to modify it . According to the characteristics of middle order traversal , You can set a prev The pointer , Start set to empty , In fact, it is the precursor node of each node , In this way, during the middle order traversal , The linked list can be connected before and after , In this way, the bidirectional linked list required in the title can be realized . Finally, according to the requirements of the topic , Return the head node of the linked list . The code implementation is as follows :
cur->left = prev;
if (prev)
{
prev->right = cur;
}
prev = cur;
Code implementation
void InOrder(TreeNode* cur, TreeNode*& prev)// In the sequence traversal
{
if (cur == nullptr)// If it is empty, it will return to
{
return;
}
InOrder(cur->left, prev);
cur->left = prev;
if (prev)
{
prev->right = cur;
}
prev = cur;
InOrder(cur->right, prev);
}
TreeNode* Convert(TreeNode* pRootOfTree)
{
if (pRootOfTree == nullptr)
{
return nullptr;
}
TreeNode* prev = nullptr;
TreeNode* cur = pRootOfTree;
InOrder(cur, prev);
TreeNode* ret = pRootOfTree;
while (ret->left)
{
ret = ret->left;
}
return ret;
}

边栏推荐
- Ctfhub SQL Boolean blind annotation
- SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
- Getting started with CTF
- 力扣23题,合并K个升序链表
- 全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
- [Golang]力扣Leetcode - 349. 两个数组的交集(哈希表)
- How to learn and self-study
- LDPC 7 - 解码简单例子
- 求字符串中最大的 3 位相同数字
- SISO decoder for a general (n,n-1) SPC code(补充章节3)
猜你喜欢

力扣刷题——二叉树的最近公共祖先

牛客刷题——把字符串转换成整数

信号的处理与捕捉

SISO decoder for min sum (supplementary Chapter 2)

RadioGroup动态添加RadioButton
![[FAQs for novices on the road] about project management](/img/14/68f5e4cead5573fc932350d8d9b06e.png)
[FAQs for novices on the road] about project management

SISO Decoder for SPC (补充章节1)

On the problem that the while loop condition in keil does not hold but cannot jump out

Some problems of DC-DC bootstrap capacitor

力扣23题,合并K个升序链表
随机推荐
H.264概念
System learning typescript (V) - joint type
力扣39题组合总和
Use transformers to convert TF model to pytorch model
全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
炫酷的可视化工具:processing 初识
. Net core redis hyperloglog type
Talking about telework | community essay solicitation
SISO decoder for min sum (supplementary Chapter 2)
TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生
[c language] output the average score and the student data below or equal to the average score with the structure
了解一下random库·1
Force deduction 32 questions longest valid bracket
牛客刷题——Fibonacci数列
Apipost精妙使用技巧
使用mysql判断日期是星期几
.net core redis hyperloglog类型
IEDA 底层菜单菜单简介
TR-069 protocol introduction
力扣23题,合并K个升序链表