230. Kth Smallest Element in a BST | 第k个最小值
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int kthSmallest(TreeNode root) {
Stack<TreeNode> s = new Stack<>();
while(root!=null || !s.isEmpty()) {
while(root!=null) {
s.push(root);
root = root.left;
}
root = s.pop();
if(--k == 0) break;
root = root.right;
}
return root.val;
}
}