Non-recursive Inorder traverse


  • 0
    J
    int kthSmallest(TreeNode* root, int k) {
        list<TreeNode*> s;
        int rank = 1;
    
        while(!s.empty() || root) {
            while(root) {
                s.push_back(root);
                root = root->left;
            }
            
            root = s.back()->right;
            if(rank == k)
                return s.back()->val;
            
            s.pop_back();
            rank++;
        }
        
        return INT_MAX;
    }

Log in to reply
 

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