LEETCODE-Kth Smallest Element in a BST


题目很简单只要对BST进行一次inorder就行了

class Solution {
private:
    int value=0;
    int count = 0;
    bool findflag = false;
    void inOrder(TreeNode*root, int k)
    {

        if (root&&!findflag)
        {
            kthSmallest(root->left, k);
            ++count;
            if (count == k)
            {
                value = root->val;
                findflag = true;
            }
            kthSmallest(root->right, k);
        }
    }
public:
    int kthSmallest(TreeNode* root, int k) {
        inOrder(root, k);
        return value;
    }

};

你可能感兴趣的:(LEETCODE-Kth Smallest Element in a BST)