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