C++ stack-based solution, quite concise


  • 0
    X
    int kthSmallest(TreeNode* root, int k) {
        stack<TreeNode*> togo;
        
        for(;;)
        {
            while(root)
            {
                togo.push(root);
                root = root->left;
            }
            root = togo.top();
            if(--k == 0)
            {
                return root->val;
            }
            togo.pop();
            root = root->right;
        }
        
        return togo.top()->val;
    }

Log in to reply
 

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