Example #1
0
        public static bool IsValidBst(TreeNode current, int?leftBound = null, int?rightBound = null)
        {
            if (current == null)
            {
                return(true);
            }
            if (leftBound.HasValue && current.val <= leftBound.Value)
            {
                return(false);
            }
            if (rightBound.HasValue && current.val >= rightBound.Value)
            {
                return(false);
            }

            return
                (IsValidBst(current.left, leftBound, current.val) &&
                 IsValidBst(current.right, current.val, rightBound));
        }
Example #2
0
 public bool IsValidBST(TreeNode root)
 {
     return(IsValidBst(root));
 }