• Long.MAX_VALUE, Long.MIN_VALUE: or, will fail by the test case 2147483647;
  • BST: cannot equal;
  • dfs template: terminate condition first, then recursively call function, i.e. root==null, return and root.val >= mx || root.val <= mn, return
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public boolean isValidBST(TreeNode root) {
return valid(root, Long.MAX_VALUE, Long.MIN_VALUE);
}

public boolean valid(TreeNode root, long mx, long mn) {
if (root == null) return true;
if (root.val >= mx || root.val <= mn) return false;
return valid(root.left, root.val, mn) && valid(root.right, mx, root.val);
}
}