老师好,在判断二叉树是否是BST时
https://leetcode.com/problems/validate-binary-search-tree/
有如下算法:
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null) return true;
return helper(root.left, Long.MIN_VALUE,root.val) && helper(root.right, root.val, Long.MAX_VALUE);
}
private boolean helper(TreeNode root, long min, long max){
if(root==null) return true;
if(root.val<=min || root.val >= max) return false;
return helper(root.left, min, root.val) && helper(root.right, root.val, max);
}
}
我的问题是:为什么这里定义最大最小值,使用long型,而非int型。
因为题目中给了node的限定为:
假设取到两个极值点,那么也不会越界啊,在第十行代码直接判断等于max或者min,返回false了,所以不知道这里是为什么要扩大到long型的64位