当前位置:网站首页>230. The k-th smallest element in the binary search tree
230. The k-th smallest element in the binary search tree
2022-07-21 22:41:00 【Anji_ lh1029】
1、 Title Description
2、 Topic analysis
To find the binary search tree, sort in k The location of the elements , Equivalent to the need to search the binary tree 【 Ascending 】 Sort .
That is, the corresponding transformation is to transform the binary search tree , Convert to 【 Middle preface 】 Traverse , And stored in a container , It can be list, Array , Or hash table , Queues are OK .
Then get page k A place , For example, I use list, index Subscript from 0 Start , So the first k Elements , That is, get the subscript k-1 The elements of ,list.get(k-1); The code is as follows :
class Solution {
// The binary tree is traversed in middle order
List<Integer> list = new ArrayList<Integer>();
public int kthSmallest(TreeNode root, int k) {
if(root == null) return -1;
// here list In ascending order
dfs(root);
if(list.size() >= k)
return list.get(k-1);
return -1;
}
private void dfs(TreeNode root){
if(root == null) return;
dfs(root.left);
list.add(root.val);
dfs(root.right);
}
}
Complexity analysis :
Time complexity O(N): In the sequence traversal , Each element is traversed to , So the complexity is O(N).
Spatial complexity O(N):list Contains all elements , So the space complexity is zero O(N).
On the basis of the above, optimize the time and space complexity , That is, once you get the k Elements , Stop . There is no need to traverse all to the element .
class Solution {
int res;
int sortK; // After sorting to k Elements , This needs to be a global variable
public int kthSmallest(TreeNode root, int k) {
//【 In the sequence traversal 】
this.sortK = k;
dfs(root);
return res;
}
private void dfs(TreeNode root){
if(root == null || sortK <= 0) return;
dfs(root.left);
if(--sortK == 0) res = root.val;
dfs(root.right);
}
}
Complexity analysis :
Time complexity : Make h For the height of the tree , Reach the leaf position first ( Minimum node position ), The complexity is O(h), And find number one k Small elements , The complexity is O(k). whole The complexity is O(h+k)
Spatial complexity : Make h For the height of the tree , The complexity is O(h)
边栏推荐
猜你喜欢
一文讲透,分布式系统的数据分片难题
What is a direct drinking machine? What is its working principle and advantages?
Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
437. 路径总和 III
Combinatorial summary
Expression evaluation
Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
XML详解
表达式求值
随机推荐
深入剖析斐波拉契数列
1046. 最后一块石头的重量
1046. Weight of the last stone
Level 3 academic level test
Robotframework -- implementation of browser drive and operation (1)
Chat about matter protocol (original chip protocol)
01- fundamentals of automated testing -- (selenium eight part + environment configuration + eight positioning)
What are the characteristics and application fields of Bluetooth module in the Internet of things?
Expression evaluation
寒假学习总结
04 unittest unit test framework
005:整型数据类型存储空间大小
organize a team
P/NP/NP完全/NP难
归并排序
2022ACM夏季集训周报(三)
62. Different paths
Linear regression finale (ridge, Lasso regression principle, formula derivation), you want everything here
234. 回文链表
2022ACM夏季集训周报(二)