Iterative inorder to get Kth smallest element


#1

void inorder(TreeNode A,vector &res){
stack<TreeNode > s;
TreeNode
curr = A;
while(curr!=NULL||!s.empty()){
while(curr!=NULL){
s.push(curr);
curr=curr->left;
}
curr=s.top();
s.pop();
res.push_back(curr->val);
curr=curr->right;
}
}
int Solution::kthsmallest(TreeNode
A, int B) {
vector res;
inorder(A,res);
return res[B-1];
}