4 line C++ simple solution, easy understanding

  • 5
     bool isValidBST(TreeNode* root) {
        return dfs_valid(root, LONG_MIN, LONG_MAX);
    bool dfs_valid(TreeNode *root, long low, long high) {
        if (!root) return true;
        return low < root->val && root->val < high && dfs_valid(root->left, low, root->val)
                && dfs_valid(root->right, root->val, high);

Log in to reply

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