soulmachine / leetcode

LeetCode题解,151道题完整版。
BSD 3-Clause "New" or "Revised" License
11.27k stars 3.43k forks source link

Validate Binary Search Tree #54

Open vendisky opened 9 years ago

vendisky commented 9 years ago

Consider the case: Only root node, whose value is INT_MAX. The code will output false instead of true.

Solution: Use LLONG_MAX to replace INT_MAX.

class Solution { public: bool isValidBST(TreeNode* root) { return isValidBST(root, LLONG_MIN, LLONG_MAX); } bool isValidBST(TreeNode* root, long long lower, long long upper) { if (!root) return true; return root->val > lower && root->val < upper && isValidBST(root->left, lower, root->val) && isValidBST(root->right, root->val, upper); } };