Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1202 from razer96/98-Validate-Binary-S…
Browse files Browse the repository at this point in the history
…earch-Tree.go

98. Validate Binary Search Tree go solution
  • Loading branch information
dissesmac authored Sep 30, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents a0f7371 + ae147f0 commit 0a4d439
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions go/98-Validate-Binary-Search-Tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func isValidBST(root *TreeNode) bool {
return isValid(root, nil, nil)
}

func isValid(root, min, max *TreeNode) bool {
if root == nil {
return true
}

if min != nil && root.Val <= min.Val {
return false
}

if max != nil && root.Val >= max.Val {
return false
}

return isValid(root.Left, min, root) && isValid(root.Right, root, max)
}

0 comments on commit 0a4d439

Please sign in to comment.