Skip to content

Commit dbba78c

Browse files
committed
add solution for validate binary search tree
1 parent dfbc331 commit dbba78c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package youngDaLee
2+
3+
import "math"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func isValidBST(root *TreeNode) bool {
12+
return dfs(root, math.MinInt, math.MaxInt)
13+
}
14+
15+
func dfs(node *TreeNode, min, max int) bool {
16+
if node == nil {
17+
return true
18+
}
19+
if (min < node.Val && node.Val < max) == false {
20+
return false
21+
}
22+
23+
return dfs(node.Left, min, node.Val) && dfs(node.Right, node.Val, max)
24+
}

0 commit comments

Comments
 (0)