Kth Smallest Element in a BST 2021-01-12 03:50
public static int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
int curIndex = 0;
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
if(curIndex==k-1){
return cur.val;
}
curIndex++;
cur = cur.right;
}
}
return -1;
}
| Runtime | Memory |
|---|---|
| 0 ms | 39.3 MB |
EOF