From f9adafdbcdedfc9fb826ef51b8cf0e58aeaf3e50 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 10:19:25 +0800 Subject: [PATCH] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index baa3f435..ddd634b4 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -260,7 +260,25 @@ Python: Go: +```Go +import "math" +func isValidBST(root *TreeNode) bool { + if root == nil { + return true + } + return isBST(root, math.MinInt64, math.MaxFloat64) +} +func isBST(root *TreeNode, min, max int) bool { + if root == nil { + return true + } + if min >= root.Val || max <= root.Val { + return false + } + return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max) +} +```