230. Kth Smallest Element in a BST
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.
Example 1:
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 35/ \3 6/ \2 4/1 Output: 3Follow 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?
難度:medium
題目:給定二叉搜索樹,找出其值第K小的結點。
思路:中序遍歷
Runtime: 0 ms, faster than 100.00% of Java online submissions for Kth Smallest Element in a BST.
Memory Usage: 38.9 MB, less than 19.71% of Java online submissions for Kth Smallest Element in a BST.
總結
以上是生活随笔為你收集整理的230. Kth Smallest Element in a BST的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css二
- 下一篇: 做梦梦到老太太拉屎啥意思