当前位置:网站首页>[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
[flax high frequency question] leetcode 426 Convert binary search tree to sorted double linked list
2022-07-03 22:08:00 【Ben potato】
Title Description & link
Leetcode 426 : take BST The structure is transformed into a double linked list structure , Make each node connect its predecessor node and successor node , And head , The last two nodes should also be connected . No extra space required
Topic ideas
I'll give you a compliment on this question , Let me traverse the middle order DFS The template has a deeper understanding . First of all, what if you can open additional space ? Think that there is a hidden property of order traversal in binary trees : In the middle order traversal, the point on the current node is the precursor node , The next point is the successor node . Then you can put BST The structure is traversed in medium order , Save and traverse the array so that each point is connected to the left and right .
What should I do if I don't use extra space . Let's take a look first BST Middle order traversal template , as follows
private void dfs(Node root) {
if(root==null) return;
dfs(root.left);
// The following is the processing area - Displays the current node
System.out.println(root.val);
//
dfs(root.right);
}We can start processing every time out recursion , Connect the precursor node with the current node , Then connect the head and tail nodes . The whole idea , First, you need to save the header , The last two nodes , First entry in recursion " Processing area " Is the first node , The last entry in recursion " Processing area " It's the last node . How to get the precursor node in recursion , Define a global variable , Save the current node after each processing logic in the processing area , So the next time you enter the processing area , The saved node is the precursor node of this node .
Processing area logic , Connect the current node with the predecessor node , The overall code is as follows :
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val,Node _left,Node _right) {
val = _val;
left = _left;
right = _right;
}
};
*/
class Solution {
Node first = null;
Node last = null;
public Node treeToDoublyList(Node root) {
if(root==null) return null;
// The middle order traverses the previous node and the next node
dfs(root);
first.left = last;
last.right = first;
return first;
}
private void dfs(Node root) {
if(root==null) return;
dfs(root.left);
// do something
// The first point entering the processing area must be the first point ( Leftmost point )
if(last!=null) {
// Somewhere in the middle
// The first point is last
last.right = root;
root.left = last;
}
if(first==null) first = root;
last = root; // last Keep updating until the last entry, that is, the rightmost point
dfs(root.right);
}
} Time complexity :
; Spatial complexity :
Recursion depth .
边栏推荐
- Nacos common configuration
- Mysql database - Advanced SQL statement (I)
- gslb(global server load balance)技术的一点理解
- DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
- (POJ - 2912) rochambau (weighted concurrent search + enumeration)
- The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
- [secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
- What is the difference between res.send() and res.end() in the node express framework
- Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
- Plug - in Oil Monkey
猜你喜欢

Redis single thread and multi thread

Minio deployment

How PHP gets all method names of objects

QGIS grid processing DEM data reclassification

Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy

How PHP drives mongodb
![[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)](/img/6c/2d48d441fee1981a271319fd9f6c23.jpg)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)

Asynchronous artifact: implementation principle and usage scenario of completable future

On my first day at work, this API timeout optimization put me down!

Nacos common configuration
随机推荐
Control loop of program (while loop)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
An expression that regularly matches one of two strings
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
鹏城杯 WEB_WP
On my first day at work, this API timeout optimization put me down!
How to install sentinel console
DOM light switch case
The latest analysis of crane driver (limited to bridge crane) in 2022 and the test questions and analysis of crane driver (limited to bridge crane)
Code in keil5 -- use the code formatting tool astyle (plug-in)
Collections SQL communes
A little understanding of GSLB (global server load balance) technology
Preliminary understanding of C program design
js demo 計算本年度還剩下多少天
JS Demo calcule combien de jours il reste de l'année
js demo 计算本年度还剩下多少天
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
股票炒股开户注册安全靠谱吗?有没有风险的?
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Base ring tree Cartesian tree