Skip to content

Commit

Permalink
go implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed May 15, 2019
1 parent 052824a commit 0618e64
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions 98. Validate Binary Search Tree/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
还有不需要min,max的一种方法:[[https://www.cnblogs.com/grandyang/p/4298435.html][C++ 解法三:]]
26 changes: 26 additions & 0 deletions 98. Validate Binary Search Tree/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package leetcode

import (
"math"
)

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func isValidBST(root *TreeNode) bool {
maxInt, minInt := math.MaxInt64, math.MinInt64
return isValid(root, minInt, maxInt)
}

func isValid(root *TreeNode, min, max int) bool {
if root == nil {
return true
}
if root.Val <= min || root.Val >= max {
return false
}
return isValid(root.Left, min, root.Val) && isValid(root.Right, root.Val, max)
}

0 comments on commit 0618e64

Please sign in to comment.