My c++ code clean


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

Log in to reply
 

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