leetcode Kth Smallest Element in a BST
思路:在二叉搜索树中,中根遍历后就是他们从小到大的排序,求第k小只需将中根遍历做小小修改即可
int cnt = 0;
int res = -1;
public void recursion(TreeNode root, int k) {
if(root == null)
return ;
recursion(root.left, k);
cnt++;
if(cnt == k)
res = root.val;
recursion(root.right, k);
}
public int kthSmallest(TreeNode root, int k) {
recursion(root, k);
return res;
}