llllmz

导航

98. 验证二叉搜索树c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 
bool judge(struct TreeNode* root,long* pre){
    if(!root) return true;
    bool a=judge(root->left,pre);
    if(root->val <= *pre) return false;
    *pre=root->val;
    bool b=judge(root->right,pre);

    return a&&b;
}


bool isValidBST(struct TreeNode* root) {
    if(!root) return true;
    if(!root->left&&!root->right ) return true;
    long pre=LONG_MIN;
    return judge(root,&pre);
}

结果:

posted on 2024-03-06 13:59  神奇的萝卜丝  阅读(27)  评论(0)    收藏  举报