二叉搜索树的第k个结点

class Solution {
public:
    TreeNode* res=NULL;
    void mid(TreeNode* root, int k,int &cnt)
    {
        if(!root)
            return;
        mid(root->left,k,cnt);
        cnt++;
        if(cnt==k)  res=root;
        mid(root->right,k,cnt);
    }
    TreeNode* kthNode(TreeNode* root, int k) {
        int cnt=0;
        mid(root,k,cnt);
        return res;
    }
};
posted @ 2023-05-07 17:00  穿过雾的阴霾  阅读(15)  评论(0)    收藏  举报