Inorder traversal is from small to big


  • 0
    H
    int kthSmallest(TreeNode* root, int k) {
        // inorder traversal is from small to big
        int count = k;
        inorder(root, count);
        return val;
    }
    
    int val = INT_MIN;
    
    void inorder(TreeNode* root, int& count) {
        if (NULL == root) return;
        inorder(root->left, count);
        if (--count == 0) { val = root->val; return; }
        inorder(root->right, count);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.