class Solution {
public int kthSmallest(TreeNode root, int k) {
if (root == null){
return -1;
}
Deque<TreeNode> stack = new ArrayDeque<>();
while(root != null){
stack.addFirst(root);
root = root.left;
}
while(!stack.isEmpty()){
TreeNode cur = stack.removeFirst();
//Situation
k--;
if(k == 0){
return cur.val;
}
//正常的Inorder遍历
cur = cur.right;
while(cur != null){
stack.addFirst(cur);
cur = cur.left;
}
}
return -1;
}
}
//Recursion的写法
private int count;
private int value;
public int kthSmallest(TreeNode root, int k){
if (root == null){
return -1;
}
this.count = k;
return this.value;
}
public int helper(TreeNode root){
if (root == null){
return ;
}
helper(root.left);
this.count--;
if (count==0){
this.value = root.val;
return;
}
helper(root.right);
return ;
}