From ac1e326cbf15017d2a98d53620016f3bb6069421 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sun, 13 Jun 2021 22:17:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200700.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0700.二叉搜索树中的搜索 go版本 --- problems/0700.二叉搜索树中的搜索.md | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 25a06617..16b21f26 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -241,6 +241,56 @@ class Solution: Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //递归法 +func searchBST(root *TreeNode, val int) *TreeNode { + if root==nil||root.Val==val{ + return root + } + if root.Val>val{ + return searchBST(root.Left,val) + } + return searchBST(root.Right,val) +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + //迭代法 +func searchBST(root *TreeNode, val int) *TreeNode { + for root!=nil{ + if root.Val>val{ + root=root.Left + }else if root.Val