当前位置:网站首页>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
边栏推荐
- USB type-C docking design | design USB type-C docking scheme | USB type-C docking circuit reference
- FIR filter of IQ signal after AD phase discrimination
- About snake equation (3)
- Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
- 解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- 2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
- 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
- Chapter VIII integrated learning
- Redis 主从复制
- General configuration toolbox
猜你喜欢

COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model

About snake equation (2)

The combination of relay and led small night light realizes the control of small night light cycle on and off

Chapter 7 Bayesian classifier

2021 welder (primary) examination skills and welder (primary) operation examination question bank

Transportation, new infrastructure and smart highway

USB type-C mobile phone projection scheme | USB type-C docking station scheme | TV / projector type-C converter scheme | ag9300ag9310ag9320

Understanding of maximum likelihood estimation

Use "recombined netlist" to automatically activate eco "APR netlist"

Basic realization of line chart (II)
随机推荐
Multi purpose signal modulation generation system based on environmental optical signal detection and user-defined signal rules
Chapter 16 intensive learning
Plot function drawing of MATLAB
A speed Limited large file transmission tool for every major network disk
Redis 主从复制
Cs5212an design display to VGA HD adapter products | display to VGA Hd 1080p adapter products
Design method and reference circuit of type C to hdmi+ PD + BB + usb3.1 hub (rj45/cf/tf/ sd/ multi port usb3.1 type-A) multifunctional expansion dock
Guojingxin center "friendship and righteousness" - the meta universe based on friendship and friendship, and the parallel of "honguniverse"
2021 Shanghai safety officer C certificate examination registration and analysis of Shanghai safety officer C certificate search
Transportation, new infrastructure and smart highway
Understanding of sidelobe cancellation
Introduction to natural language processing (NLP) based on transformers
Cs5261type-c to HDMI alternative ag9310 | ag9310 alternative
Capstone/cs5210 chip | cs5210 design scheme | cs5210 design data
FIR filter of IQ signal after AD phase discrimination
Qt - - Packaging Programs - - Don't install Qt - can run directly
High quality USB sound card / audio chip sss1700 | sss1700 design 96 kHz 24 bit sampling rate USB headset microphone scheme | sss1700 Chinese design scheme explanation
Probability distribution
2022 high voltage electrician examination skills and high voltage electrician reexamination examination
Coordinate conversion of one-dimensional array and two-dimensional matrix (storage of matrix)