ValidateBST easy solution but not very fast!?!


  • 0
    N

    I have the following solution but it beats only 30% of C++ solutions. Anyone has any idea how I can make it faster?
    Thanks

    public:
        vector<int> list;
        bool isValidBST(TreeNode* root) {
            bool left = true;
            bool right = true;
            bool node = true;
            if (root == NULL) return true;
            else{
                if(root->left != NULL){
                    left = isValidBST(root->left);
                }
                list.push_back(root->val);
                if (list.size() > 1 && list[list.size()-2] >= root->val)
                    node = false;
                if(root->right != NULL){
                    right = isValidBST(root -> right);
                }
            }
            return left & right & node;
        }
    };```

Log in to reply
 

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