LeetCode Kth Smallest Element in a BST
LeetCode in Element BST Smallest
2023-09-11 14:14:43 时间
Given a binary search tree, write a function kthSmallest
to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Hint:
- Try to utilize the property of a BST.
- What if you could modify the BST node's structure?
- The optimal runtime complexity is O(height of BST).
思路分析:基本就是实现BST的中序遍历。BST中序遍历得到有序的数组,能够easy知道第k小数。下面给出了递归实现,借助counter记录当前遍历过的node数目。当counter==k时就能够返回。
当然也能够借助栈实现迭代的解法。
Follow up: 进一步优化。我们能够在节点中额外保留一些信息: 左子树的大小. 在插入删除时也同一时候维护左子树的大小.进行查找时能够比較左子树size和k的大小,就能够知道要找的node在左边还是右边。通过分治法的思路加速. 时间复杂度为O(h)。
下面的伪代码摘录自 http://bookshadow.com/weblog/2015/07/02/leetcode-kth-smallest-element-bst/
假设BST节点TreeNode的属性能够扩展,则再加入一个属性leftCnt,记录左子树的节点个数
记当前节点为node
当node不为空时循环:
若k == node.leftCnt + 1:则返回node
否则。若k > node.leftCnt:则令k -= node.leftCnt + 1,令node = node.right
否则,node = node.left
上述算法时间复杂度为O(BST的高度)
当node不为空时循环:
若k == node.leftCnt + 1:则返回node
否则。若k > node.leftCnt:则令k -= node.leftCnt + 1,令node = node.right
否则,node = node.left
上述算法时间复杂度为O(BST的高度)
AC Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int res = 0; public int counter = 0; public int kthSmallest(TreeNode root, int k) { inOrderK(root, k); return res; } public void inOrderK(TreeNode root, int k){ if(root.left!=null) inOrderK(root.left, k); counter++; if(counter == k) { res = root.val; return; } if(root.right!=null) inOrderK(root.right, k); } }
相关文章
- Leetcode 之Populating Next Right Pointers in Each Node II(51)
- Leetcode 之Largest Rectangle in Histogram(40)
- leetcode 之Swap Nodes in Pairs(21)
- Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)
- Java实现 LeetCode 365 水壶问题
- Java实现 LeetCode 238 除自身以外数组的乘积
- Java实现LeetCode #986 - Interval List Intersections
- LeetCode(50):Pow(x, n)
- LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
- (LeetCode 153)Find Minimum in Rotated Sorted Array
- [LeetCode] Best Time to Buy and Sell Stock II
- 【LeetCode 85】最大矩形(第二遍)
- LeetCode - 503 下一个更大元素 II
- Matlab:成功解决In an assignment A(I)=B,the number of elements in B and I must be the same
- 已解决FutureWarning: The default value of regex will change from True to False in a future version. In
- Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode
- Leetcode 896. 单调数列
- Leetcode 492. 构造矩形
- [LeetCode] 437. 路径总和 III ☆☆☆(递归)
- LeetCode——Populating Next Right Pointers in Each Node II
- Spark SQL 源代码分析之 In-Memory Columnar Storage 之 in-memory query
- 力扣LeetCode,唯一摩尔斯密码词
- leetcode第一刷_Search in Rotated Sorted Array
- leetcode 720. Longest Word in Dictionary
- leetcode 448. Find All Numbers Disappeared in an Array
- leetcode 378. Kth Smallest Element in a Sorted Matrix
- 【LeetCode】61.旋转链表
- 【LeetCode】 2332. 坐上公交的最晚时间