当前位置:网站首页>Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
2022-07-08 01:28:00 【SK_ Jaco】
1. Title Description
The finger of the sword Offer 36. Binary search tree and double linked list
Enter a binary search tree , The binary search tree is transformed into a sorted circular bidirectional linked list . It is required that no new nodes can be created , You can only adjust the direction of the node pointer in the tree .
In order for you to better understand the problem , Take the following binary search tree as an example :
We hope to transform this binary search tree into a two-way circular linked list . Each node in the linked list has a precursor and successor pointer . For two-way circular linked list , The precursor of the first node is the last node , The last node is followed by the first node .
The following figure shows the linked list transformed from the above binary search tree .“head” Indicates the node with the smallest element in the linked list .
Specially , We want to be able to complete the conversion in place . 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 . You also need to return the pointer of the first node in the linked list .
2.1 Their thinking
Given a binary search tree , And it is required to turn it into a ordered two-way linked list on the basis of the original node , And return the node with the smallest value . According to the meaning of the question, you need to complete two steps : The first step is to sort the nodes of the binary search tree , The second step is to connect the nodes into a two-way linked list . Because the title is given as a binary search tree , Therefore, we can get the nodes from small to large by traversing the binary search tree , Using this property, we can complete the first step , Traverse and put the node in the queue , Used for assembling bidirectional linked list .
Get the ordered node queue, and then start to assemble the bidirectional linked list , The first node of the queue is the head node that needs to be returned , After the node left Point to the previous node of the current node in the queue , right Just think of the next node of the current node in the queue , Until the queue is empty . At this time, connect the head node and the tail node to complete the problem requirements .
Take the example of topic presentation , First, do the middle order traversal , Get the node queue [1, 2, 3, 4, 5]
Then get the header node from the queue , Then traverse the queue in turn , Set the previous node pre Of right Point to the current node popped out of the queue curr, And then the current node's left Point to the previous node . After this operation, you will pre Point to the current node , Continue to facilitate the queue , Until all the nodes in the queue pop up .
The last step is to connect the ending nodes , At this point, complete the assembly of the two-way circular linked list .
2.2 Code
class Solution {
Queue<Node> queue = new LinkedList<>();
public Node treeToDoublyList(Node root) {
if (root == null) {
return null;
}
process(root);
// Pop up the team head node as the head node , And use pre Point to previous node
Node head = queue.poll();
Node pre = head;
while (!queue.isEmpty()) {
// Pop the queue header from the queue , Then connect the previous node with the current node
Node curr = queue.poll();
pre.right = curr;
curr.left = pre;
pre = curr;
}
// After the queue is ejected, make a closing connection
pre.right = head;
head.left = pre;
return head;
}
public void process(Node root) {
if (root == null) {
return;
}
process(root.left);
// In the sequence traversal , Put the node in the queue
queue.offer(root);
process(root.right);
}
}
2.3 test result
Pass the test
3. summary
- Using the ergodic property of the binary search tree to obtain the sorted node queue
- Then use the node queue to assemble the bidirectional circular linked list
边栏推荐
- The solution of frame dropping problem in gnuradio OFDM operation
- Measure the voltage with analog input (taking Arduino as an example, the range is about 1KV)
- Macro definition and multiple parameters
- Gnuradio 3.9 using OOT custom module problem record
- Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
- The Ministry of housing and urban rural development officially issued the technical standard for urban information model (CIM) basic platform, which will be implemented from June 1
- Redis 主从复制
- Introduction to the types and repair methods of chip Eco
- Solve the error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
- FIR filter of IQ signal after AD phase discrimination
猜你喜欢
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Know how to get the traffic password
HDMI to VGA acquisition HD adapter scheme | HDMI to VGA 1080p audio and video converter scheme | cs5210 scheme design explanation
3、多智能体强化学习
Design method and application of ag9311maq and ag9311mcq in USB type-C docking station or converter
redis的持久化方式-RDB和AOF 两种持久化机制
qt--将程序打包--不要安装qt-可以直接运行
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
2022 high altitude installation, maintenance and demolition examination materials and high altitude installation, maintenance and demolition operation certificate examination
A speed Limited large file transmission tool for every major network disk
随机推荐
Gnuradio3.9.4 create OOT module instances
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
Gnuradio transmits video and displays it in real time using VLC
General configuration title
Call (import) in Jupiter notebook ipynb . Py file
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference
AttributeError: ‘str‘ object has no attribute ‘strftime‘
2021-03-14 - play with generics
Smart grid overview
Transportation, new infrastructure and smart highway
LaTeX 中 xcolor 颜色的用法
On the concept and application of filtering in radar signal processing
Recommend a document management tool mendely Reference Manager
The difference between distribution function and probability density function of random variables
Chapter 7 Bayesian classifier
common commands
qt--將程序打包--不要安裝qt-可以直接運行
QT -- package the program -- don't install qt- you can run it directly
Understanding of sidelobe cancellation
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills