当前位置:网站首页>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
边栏推荐
- 5. Contrôle discret et contrôle continu
- Generic configuration legend
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
- LeetCode 练习——剑指 Offer 36. 二叉搜索树与双向链表
- COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
- 3、多智能体强化学习
- Talk about smart Park
- Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
- Blue Bridge Cup embedded (F103) -1 STM32 clock operation and led operation method
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
猜你喜欢
5、离散控制与连续控制
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
Ag7120 and ag7220 explain the driving scheme of HDMI signal extension amplifier | ag7120 and ag7220 design HDMI signal extension amplifier circuit reference
Content of one frame
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
qt--將程序打包--不要安裝qt-可以直接運行
Common fault analysis and Countermeasures of using MySQL in go language
3. Multi agent reinforcement learning
Common effects of line chart
2021-04-12 - new features lambda expression and function functional interface programming
随机推荐
qt--将程序打包--不要安装qt-可以直接运行
正则表达式
Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
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
The difference between distribution function and probability density function of random variables
Gnuradio3.9.4 create OOT module instances
Recommend a document management tool Zotero | with tutorials and learning paths
Taiwan Xinchuang sss1700 latest Chinese specification | sss1700 latest Chinese specification | sss1700datasheet Chinese explanation
About snake equation (2)
5、離散控制與連續控制
Macro definition and multiple parameters
Measure the voltage with analog input (taking Arduino as an example, the range is about 1KV)
HDMI to VGA acquisition HD adapter scheme | HDMI to VGA 1080p audio and video converter scheme | cs5210 scheme design explanation
The persistence mode of redis - RDB and AOF persistence mechanisms
Frequency probability and Bayesian probability
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
Ag9310 same function alternative | cs5261 replaces ag9310type-c to HDMI single switch screen alternative | low BOM replaces ag9310 design
EDP to LVDS conversion design circuit | EDP to LVDS adapter board circuit | capstone/cs5211 chip circuit schematic reference
Redis master-slave replication
Probability distribution