当前位置:网站首页>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
边栏推荐
- About snake equation (5)
- Micro rabbit gets a field of API interface JSON
- Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
- Matlab code on error analysis (MAE, MAPE, RMSE)
- About snake equation (1)
- Introduction to the types and repair methods of chip Eco
- Introduction to natural language processing (NLP) based on transformers
- Common configurations in rectangular coordinate system
- After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
- 写一个纯手写的qt的hello world
猜你喜欢
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
3. Multi agent reinforcement learning
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
Several frequently used OCR document scanning tools | no watermark | avoid IQ tax
Talk about smart Park
break net
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
About snake equation (5)
The communication clock (electronic time-frequency or electronic time-frequency auxiliary device) writes something casually
redis的持久化方式-RDB和AOF 两种持久化机制
随机推荐
Ag9310 design USB type C to hdmi+u2+5v slow charging scheme design | ag9310 expansion dock scheme circuit | type-C dongle design data
2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
Introduction to natural language processing (NLP) based on transformers
About snake equation (2)
A speed Limited large file transmission tool for every major network disk
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
2021-03-14 - play with generics
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
npm 內部拆分模塊
Common effects of line chart
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
[loss function] entropy / relative entropy / cross entropy
Call (import) in Jupiter notebook ipynb . Py file
Redis cluster
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
FIR filter of IQ signal after AD phase discrimination
4、策略學習
2、TD+Learning
2、TD+Learning