public static int kthSmallest(TreeNode root, int k) {
Stack<TreeNode>stack=new Stack<>();
TreeNode n= root;
while(n!=null){
stack.push(n);
n=n.left;
}
while(k>0&&(n!=null||!stack.isEmpty())){
if(n==null){
n=stack.pop();
k--;
if(k==0)
return n.val;
n=n.right;
}else{
stack.push(n);
n=n.left;
}
}
return n.val;
}
230 Kth Smallest Element in a BST
最新推荐文章于 2022-08-12 12:05:29 发布