s4553711
4/23/2017 - 2:52 PM

98.cpp

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root, long min, long max) {
        if (root == NULL) return true;
        if (root->val <= min) return false;
        if (root->val >= max) return false;
        bool l_result = isValidBST(root->left, min, root->val);
        bool r_result = isValidBST(root->right, root->val, max);
        return l_result && r_result;
    }
    bool isValidBST(TreeNode* root) {
        return isValidBST(root, (long)INT_MIN-1, (long)INT_MAX+1);
    }
};