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二
- 下一篇: IDEA 插件开发入门教程