[Leetcode]Validate Binary Search Tree

来源:互联网 发布:网络优化方案 编辑:程序博客网 时间:2024/04/29 13:18
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isValidBST(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        return isValidBST(root, INT_MIN, INT_MAX);    }    bool isValidBST(TreeNode *root, int min, int max)    {        if(root == NULL) return true;        if(root->val > max || root->val < min) return false;        return isValidBST(root->left, min, root->val-1)               && isValidBST(root->right, root->val+1, max);    }};

原创粉丝点击