llllmz

导航

98. 验证二叉搜索树c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool inorder(struct TreeNode* root,long* pre){
    if(!root) return true;
    bool a=inorder(root->left,pre);
    if(root->val <= *pre) return false;
    *pre=root->val; 
    bool b=inorder(root->right,pre);
    return a&b;
}


bool isValidBST(struct TreeNode* root) {
    long pre=LONG_MIN;
    return inorder(root,&pre);
}

结果:

posted on 2024-03-14 15:21  神奇的萝卜丝  阅读(16)  评论(0)    收藏  举报