add new solutions
This commit is contained in:
parent
d798d5e8c9
commit
886b5e0a8e
34 changed files with 1164 additions and 0 deletions
39
solutions/0/q98/solution.go
Normal file
39
solutions/0/q98/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package q98
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func isValid(node *TreeNode) (bool, int, int) {
|
||||
if node.Left == nil && node.Right == nil {
|
||||
return true, node.Val, node.Val
|
||||
}
|
||||
|
||||
minv, maxv := node.Val, node.Val
|
||||
|
||||
if node.Left != nil {
|
||||
valid, lmin, lmax := isValid(node.Left)
|
||||
if !valid || lmax >= node.Val {
|
||||
return false, 0, 0
|
||||
}
|
||||
minv = lmin
|
||||
}
|
||||
if node.Right != nil {
|
||||
valid, rmin, rmax := isValid(node.Right)
|
||||
if !valid || rmin <= node.Val {
|
||||
return false, 0, 0
|
||||
}
|
||||
maxv = rmax
|
||||
}
|
||||
|
||||
return true, minv, maxv
|
||||
}
|
||||
|
||||
func isValidBST(root *TreeNode) bool {
|
||||
valid, _, _ := isValid(root)
|
||||
return valid
|
||||
}
|
||||
|
||||
var _ = isValidBST
|
||||
Loading…
Add table
Add a link
Reference in a new issue