class Solution {
public:
    TreeNode* KthNode(TreeNode* pRoot, int k)
    {
        if (pRoot == NULL || k == 0) return NULL;
        stack<TreeNode*> st;
        TreeNode* p = pRoot;
        while(!st.empty() || p) {
            while(p) {
                st.push(p);
                p = p->left;
            }
            TreeNode* t = st.top();
            st.pop();
            k--;
            if (k == 0) return t;
            p = t->right;
        }
        return NULL;
    }
};