Skip to content

Commit 71cdced

Browse files
committed
go: search in a binary search tree
1 parent 8b239a6 commit 71cdced

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

go/search-in-a-binary-search-tree.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
12+
// https://leetcode.com/problems/search-in-a-binary-search-tree/
13+
func searchBST(root *TreeNode, val int) *TreeNode {
14+
if root == nil || root.Val == val {
15+
return root
16+
}
17+
18+
if val > root.Val {
19+
return searchBST(root.Right, val)
20+
}
21+
return searchBST(root.Left, val)
22+
}

0 commit comments

Comments
 (0)