当前位置:网站首页>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;
}

边栏推荐
- MySQL/Redis 常见面试题汇总
- V-for loop traversal
- Reading summary of nacos2.x source code
- PIL-Pillow图像处理【1】-安装与新建
- Ubuntu installs PSQL and runs related commands
- Summary of common mysql/redis interview questions
- General terms in security field
- Force buckle 31 next arrangement
- async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
- [C语言]用结构体把最高分的学生输出,可有多个最高分
猜你喜欢
Summary of common mysql/redis interview questions
![[c language] output the students within the specified score range with the structure](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[c language] output the students within the specified score range with the structure
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters

Reading summary of nacos2.x source code

【无标题】

力扣刷题——根据二叉树创建字符串

牛客刷题——不要二

vim常用命令

非递归实现二叉树的前、中、后序遍历

BigDecimal基本使用与闭坑介绍
随机推荐
求字符串中最大的 3 位相同数字
The HashSet collection stores student objects and traverses
Force deduction 23 questions, merging K ascending linked lists
非递归实现二叉树的前、中、后序遍历
EditText 金额限制
单选按钮 文字背景同时改变
力扣39题组合总和
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
牛客刷题——part6
使用mysql判断日期是星期几
Ctfhub SQL Boolean blind annotation
New work of "the father of LSTM": a new method towards self correcting neural network
牛客刷题——求最小公倍数
力扣刷题——二叉树的层序遍历
Ubuntu installs PSQL and runs related commands
Reading summary of nacos2.x source code
[golang] leetcode - 349 Intersection of two arrays (hash table)
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
Summary of common mysql/redis interview questions