private static bool Helper(LeetCode98TreeNode root, int?lower, int?upper) { if (root == null) { return(true); } if (lower != null && root.val <= lower) { return(false); } if (upper != null && root.val >= upper) { return(false); } if (!Helper(root.left, lower, root.val)) { return(false); } if (!Helper(root.right, root.val, upper)) { return(false); } return(true); }
public static bool IsValidBST(LeetCode98TreeNode root) { return(Helper(root, null, null)); }
public LeetCode98TreeNode(int val = 0, LeetCode98TreeNode left = null, LeetCode98TreeNode right = null) { this.val = val; this.left = left; this.right = right; }