LeetCode 230 Kth Smallest Element in a BST 题解
1.题目:
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,寻找这个数中第K小的数。
2.解题思路:
画张图跟如下代码走一遍应该就明白了。
3.代码
1 |
|