Share my 24ms iterative C++ solution


  • 0
    Y
    class Solution {
    public:
        int kthSmallest(TreeNode* root, int k) {
        if (!root)
            return -1;
    
        stack<TreeNode*> s;
    
        while (root) {
            s.push(root);
            root = root->left;
        }
    
    
        while (k) {
            TreeNode* node;
            do {
                node = s.top();
                s.pop();
                if (--k == 0)
                    return node->val;
            } while (!node->right);
    
            node = node->right;
    
            while (node) {
                s.push(node);
                node = node->left;
            }
        }
    
        return -1;
        }
    };

Log in to reply
 

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