当前位置:网站首页>[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 .
边栏推荐
- Preliminary understanding of C program design
- Introduction to kubernetes
- Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
- Kali2021.4a build PWN environment
- Is the account opening of Guotai Junan Securities safe and reliable? How to open Guotai Junan Securities Account
- Nacos common configuration
- Leetcode problem solving - 235 Nearest common ancestor of binary search tree
- 十大券商开户注册安全靠谱吗?有没有风险的?
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
- (POJ - 2912) rochambau (weighted concurrent search + enumeration)
猜你喜欢

How PHP drives mongodb
![Intimacy communication -- [repair relationship] - use communication to heal injuries](/img/c2/f10405e3caf570dc6bd124d65b2e93.jpg)
Intimacy communication -- [repair relationship] - use communication to heal injuries

The post-90s resigned and started a business, saying they would kill cloud database

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

QGIS grid processing DEM data reclassification

常用sql集合

Common SQL sets

Dahua series books

gslb(global server load balance)技术的一点理解

Electronic tube: Literature Research on basic characteristics of 6j1
随机推荐
国泰君安证券开户是安全可靠的么?怎么开国泰君安证券账户
Morning flowers and evening flowers
Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
Sed、Awk
Global and Chinese market of telematics boxes 2022-2028: Research Report on technology, participants, trends, market size and share
QGIS grid processing DEM data reclassification
Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
Redis concludes that the second pipeline publishes / subscribes to bloom filter redis as a database and caches RDB AOF redis configuration files
An expression that regularly matches one of two strings
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
2022 safety officer-b certificate examination summary and safety officer-b certificate simulation test questions
Dahua series books
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
Conditional statements of shell programming
Electronic tube: Literature Research on basic characteristics of 6j1
Control loop of program (while loop)
WFC900M-Network_ Card/Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-900M-high-power-Mini-PCIe-Wi-Fi-Mod
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
js demo 計算本年度還剩下多少天