验证二叉搜索树(BST) Leetcode98题(巨简洁)(Verify binary search tree (BST) leetcode 98 question (giant concise))

long pre = LONG_MIN;
bool isValidBST(struct TreeNode* root){
    if(!root)return true;

    if(!(isValidBST(root->left))) return false;
    if(root->val <= pre)return false;

    pre = root->val;
    
    return isValidBST(root->right);
}

 变量pre 用Long是因为root的值可能是INT_MIN,用INT的话会判断失败 (血的教训 = =!)

 另外,pre要用全局变量,否则的话

isValidBST(root->right)会读不进去上一个pre,右子树的左子树如果小于上一个节点的话就会直接被跳过。
————————
long pre = LONG_MIN;
bool isValidBST(struct TreeNode* root){
    if(!root)return true;

    if(!(isValidBST(root->left))) return false;
    if(root->val <= pre)return false;

    pre = root->val;
    
    return isValidBST(root->right);
}

The variable pre uses long because the value of root may be int_ Min, if int is used, failure will be judged (lesson of blood = =!)

In addition, pre should use global variables, otherwise

isValidBST(root->right)会读不进去上一个pre,右子树的左子树如果小于上一个节点的话就会直接被跳过。