Open
Description
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);
}
};
Metadata
Metadata
Assignees
Labels
No labels