Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

剑指 Offer 54. 二叉搜索树的第k大节点 #292

Open
MyLinChi opened this issue Sep 14, 2020 · 0 comments
Open

剑指 Offer 54. 二叉搜索树的第k大节点 #292

MyLinChi opened this issue Sep 14, 2020 · 0 comments

Comments

@MyLinChi
Copy link
Owner

问题

给定一棵二叉搜索树,请找出其中第k大的节点。

求解

搜索二叉树的中序遍历是有序的。

class Solution {
private:
    bool flag;
    int k;
    int res;
    void treeCount(TreeNode* root) {
        if (root == nullptr)return;
        treeCount(root->right);
        if(--k==0)res = root->val;
        treeCount(root->left);
    }
public:
    int kthLargest(TreeNode* root, int k) {
        flag = false;
        this->k = k;
        treeCount(root);
        return res;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant