add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent d798d5e8c9
commit 886b5e0a8e
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
34 changed files with 1164 additions and 0 deletions

View 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