图片说明

  1. 注意k == 0 的边界条件
  2. 中序遍历时升序数列。
/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:

    vector<TreeNode*> res;


    void dfs(TreeNode* pRoot){
       if(!pRoot){
           return;
       }


        dfs(pRoot->left);

        res.push_back(pRoot);

        dfs(pRoot->right);

    }


    TreeNode* KthNode(TreeNode* pRoot, int k) {

        if(!pRoot||k==0){
            return NULL;
        }

        dfs(pRoot);

        return res[k-1];

    }


};