llllmz

导航

98. 验证二叉搜索树c

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

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

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

 

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