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.
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?
题目大意
找二叉搜索树中,第 k
个小的值。
解题思路
通过辅助栈,按照中序遍历二叉树。
代码
|
|