Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate Binary Search Tree #54

Open
vendisky opened this issue Feb 22, 2015 · 0 comments
Open

Validate Binary Search Tree #54

vendisky opened this issue Feb 22, 2015 · 0 comments

Comments

@vendisky
Copy link

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);
}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant