62.二叉树搜索树的第K个节点——剑指offer

 

 

 

typedef TreeNode* pnode;
class Solution {
    int m;
    pnode ans;
    void dfs(pnode p){
        if(!p || m < 1) return;
        dfs(p -> left);
        if(m == 1) ans = p;
        --m;
        if(m > 0) dfs(p -> right);
    }
public:
    TreeNode* KthNode(TreeNode* p, unsigned int k){
        ans = NULL; m = k;
        dfs(p);
        return ans; 
    }
};

 

posted @ 2019-07-09 21:50  unique_ptr  阅读(117)  评论(0编辑  收藏  举报