230. 二叉搜索树中第K小的元素
8/3/23Less than 1 minute
230. 二叉搜索树中第K小的元素
中等解法:中序遍历
class Solution {
int num = 0;
int res;
public int kthSmallest(TreeNode root, int k) {
if(root.left != null) {
kthSmallest(root.left, k);
}
num++;
if(num == k) res = root.val;
if(root.right != null) {
kthSmallest(root.right, k);
}
return res;
}
}