6ms solution in C - inorder traverse


  • 0
    C

    although, space overhead is somewhat big~

    void in_tra(struct TreeNode * root, int * stack, int * top)
    {
        if(!root) return;
        in_tra(root->left, stack, top);
        stack[(*top)++] = root->val;
        in_tra(root->right, stack, top);
    }
    
    
    bool isValidBST(struct TreeNode *root)
    {
        int stack[10000];
        int top = 0;
        int i;
    
        in_tra(root, stack, &top);
    
        for(i = 1; i < top; ++i)
        {
            if(stack[i] <= stack[i - 1]) return false;
        }
        return true;
    }

Log in to reply
 

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