From a85036e81126de1b026772ef803776a56540946c Mon Sep 17 00:00:00 2001 From: Yuhao Ju Date: Sun, 4 Dec 2022 12:24:34 +0800 Subject: [PATCH] =?UTF-8?q?update=200700.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2:=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20go=20=E7=9A=84=E8=BF=AD=E4=BB=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 2ee2bdb0..f40f778a 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -24,7 +24,7 @@ ## 思路 -之前我们讲了都是普通二叉树,那么接下来看看二叉搜索树。 +之前我们讲的都是普通二叉树,那么接下来看看二叉搜索树。 在[关于二叉树,你该了解这些!](https://programmercarl.com/二叉树理论基础.html)中,我们已经讲过了二叉搜索树。 @@ -269,13 +269,13 @@ class Solution: ```go //递归法 func searchBST(root *TreeNode, val int) *TreeNode { - if root==nil||root.Val==val{ + if root == nil || root.Val == val { return root } - if root.Val>val{ - return searchBST(root.Left,val) + if root.Val > val { + return searchBST(root.Left, val) } - return searchBST(root.Right,val) + return searchBST(root.Right, val) } ``` @@ -284,13 +284,13 @@ func searchBST(root *TreeNode, val int) *TreeNode { ```go //迭代法 func searchBST(root *TreeNode, val int) *TreeNode { - for root!=nil{ - if root.Val>val{ - root=root.Left - }else if root.Val val { + root = root.Left + } else if root.Val < val { + root = root.Right + } else { + return root } } return nil