Skip to content

Commit ae147f0

Browse files
author
dfad
committed
98. Validate Binary Search Tree go solution
1 parent 2c65a33 commit ae147f0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func isValidBST(root *TreeNode) bool {
2+
return isValid(root, nil, nil)
3+
}
4+
5+
func isValid(root, min, max *TreeNode) bool {
6+
if root == nil {
7+
return true
8+
}
9+
10+
if min != nil && root.Val <= min.Val {
11+
return false
12+
}
13+
14+
if max != nil && root.Val >= max.Val {
15+
return false
16+
}
17+
18+
return isValid(root.Left, min, root) && isValid(root.Right, root, max)
19+
}

0 commit comments

Comments
 (0)