Best Solution in C++ (iterative solution)


#1
int Solution::kthsmallest(TreeNode* A, int B) {
stack<TreeNode*>st;
TreeNode *p=A,*k;
int count=0;
while(count<B){
    while(p){
        st.push(p);
        p=p->left;
    }
    p=st.top();
    st.pop();
    count++;
    k=p;
    p=p->right;
}
return k->val;

}