当前位置:网站首页>[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 .
边栏推荐
- Cognitive fallacy: what is Fredkin's paradox
- How to store null value on the disk of yyds dry inventory?
- 6.0 kernel driver character driver
- Functions and differences between static and Const
- Development trend and market demand analysis report of China's energy storage battery industry Ⓩ 2022 ~ 2028
- JS Demo calcule combien de jours il reste de l'année
- Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
- Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
- Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)
- 仿网易云音乐小程序
猜你喜欢
(5) User login - services and processes - History Du touch date stat CP
Collection | pytoch common loss function disassembly
Functions and differences between static and Const
Electronic tube: Literature Research on basic characteristics of 6j1
[SRS] build a specified version of SRS
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Asynchronous artifact: implementation principle and usage scenario of completable future
常用sql集合
[dynamic planning] counting garlic customers: the log of garlic King (the longest increasing public subsequence)
[secretly kill little partner pytorch20 days] - [day3] - [example of text data modeling process]
随机推荐
English topic assignment (28)
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)
JS notes (III)
[dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
Yiwen teaches you how to choose your own NFT trading market
Code in keil5 -- use the code formatting tool astyle (plug-in)
Redis single thread and multi thread
Décompiler et modifier un exe ou une DLL non source en utilisant dnspy
What indicators should be paid attention to in current limit monitoring?
常用sql集合
Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy
Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
2022-02-15 Daily: 2022 AAAI fellow release
Base ring tree Cartesian tree
Oil monkey plug-in
Supply and demand situation and market scale calculation report of China's portable energy storage power PES industry Ⓛ 2022 ~ 2028
Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
Idea shortcut word operation
Decompile and modify the non source exe or DLL with dnspy