Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 857ec12

Browse files
Merge pull request youngyangyang04#929 from iiiuwioajdks/master
FIX: 更正0098.验证二叉搜索树go语言版的代码
2 parents e382105 + 3ae922f commit 857ec12

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

‎problems/0098.验证二叉搜索树.md‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,19 +414,24 @@ class Solution:
414414
import "math"
415415

416416
func isValidBST(root *TreeNode) bool {
417-
if root == nil {
418-
return true
419-
}
420-
return isBST(root, math.MinInt64, math.MaxFloat64)
417+
// 二叉搜索树也可以是空树
418+
if root == nil {
419+
return true
420+
}
421+
// 由题目中的数据限制可以得出min和max
422+
return check(root,math.MinInt64,math.MaxInt64)
421423
}
422-
func isBST(root *TreeNode, min, max int) bool {
423-
if root == nil {
424-
return true
425-
}
426-
if min >= root.Val || max <= root.Val {
427-
return false
428-
}
429-
return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max)
424+
425+
func check(node *TreeNode,min,max int64) bool {
426+
if node == nil {
427+
return true
428+
}
429+
430+
if min >= int64(node.Val) || max <= int64(node.Val) {
431+
return false
432+
}
433+
// 分别对左子树和右子树递归判断,如果左子树和右子树都符合则返回true
434+
return check(node.Right,int64(node.Val),max) && check(node.Left,min,int64(node.Val))
430435
}
431436
```
432437
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /