Simple c++ solution, only one more line than the code of BST inorder traversal


  • 0
    I
    int kthSmallest(TreeNode* root, int k) {
        stack<TreeNode *> stck;
        while (root || !stck.empty()) {
            while (root) {
                stck.push(root);
                root = root->left;
            }
            root = stck.top();
            stck.pop();
            if (--k == 0) return root->val;
            root = root->right;
        }
        return 0;
    }

Log in to reply
 

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