C++ concise iterative solution


  • 0
    L
    int kthSmallest(TreeNode* root, int k)
        {
            stack<TreeNode *> stk;
            while (root || stk.size())
            {
                if (root)
                {
                    stk.push(root);
                    root = root->left;
                }
                else
                {
                    TreeNode *now = stk.top();
                    stk.pop();
                    if (--k == 0) return now->val;
                    root = now->right;
                }
            }
        }
    

Log in to reply
 

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