当前位置:网站首页>[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 .
边栏推荐
- Sed、Awk
- (5) User login - services and processes - History Du touch date stat CP
- Yiwen teaches you how to choose your own NFT trading market
- Bluebridge cup Guoxin Changtian single chip microcomputer -- hardware environment (I)
- Miscellaneous things that don't miss the right business
- Investment analysis and prospect trend prediction report of China's boron nitride industry Ⓨ 2022 ~ 2028
- Farmersworld farmers world, no faith, how to talk about success?
- TiDB 之 TiCDC6.0 初体验
- Global and Chinese market of AC induction motors 2022-2028: Research Report on technology, participants, trends, market size and share
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
猜你喜欢

What is the difference between res.send() and res.end() in the node express framework

How PHP drives mongodb

gslb(global server load balance)技術的一點理解

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

6.0 kernel driver character driver

Teach you how to install aidlux (1 installation)

How PHP adds two numbers

Nacos common configuration

2022 electrician (elementary) examination questions and electrician (elementary) registration examination

Code in keil5 -- use the code formatting tool astyle (plug-in)
随机推荐
90 後,辭職創業,說要卷死雲數據庫
Exclusive interview with the person in charge of openkruise: to what extent has cloud native application automation developed now?
Mysql database - Advanced SQL statement (I)
DR-NAS26-Qualcomm-Atheros-AR9582-2T-2R-MIMO-802.11-N-5GHz-high-power-Mini-PCIe-Wi-Fi-Module
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Redis single thread and multi thread
Conditional statements of shell programming
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
[dynamic programming] Jisuan Ke: Jumping stake (variant of the longest increasing subsequence)
DR-AP40X9-A-Qualcomm-IPQ-4019-IPQ-4029-5G-4G-LTE-aluminum-body-dual-band-wifi-router-2.4GHZ-5GHz-QSD
What is the content of the securities practice examination?
Covariance
[sg function] lightoj Partitioning Game
Common SQL sets
Report on the current situation and development trend of ethoxylated sodium alkyl sulfate industry in the world and China Ⓞ 2022 ~ 2027
MySQL——JDBC
Imitation Netease cloud music applet
Code in keil5 -- use the code formatting tool astyle (plug-in)
Yiwen teaches you how to choose your own NFT trading market
Blue Bridge Cup Guoxin Changtian single chip microcomputer -- software environment (II)