1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution { public boolean isValidBST(TreeNode root) { return helper(root, null, null); }
private boolean helper(TreeNode root, Integer lower, Integer upper) { if (root == null) { return true; } int val = root.val; if ((lower != null && val <= lower) || (upper != null && val >= upper)) { return false; } return helper(root.right, val, upper) && helper(root.left, lower, val); } }
|