const long long Infinity = 9223372036854775807;
const long long negInfinity = -9223372036854775800;
class Solution {
public:
bool helper(TreeNode* root, long long lower, long long upper)
{
if (root == nullptr)
return true;
if (root->val <= lower || root->val >= upper)
return false;
return (helper(root->left, lower, root->val) && helper(root->right, root->val, upper));
}
bool isValidBST(TreeNode* root) {
return helper(root, negInfinity, Infinity);
}
};
LeetCode98.验证二叉搜索树
最新推荐文章于 2024-11-10 21:54:30 发布