leet-code/search-in-a-binary-search-tree/sol.go

30 lines
430 B
Go
Raw Normal View History

2022-06-09 14:45:14 +00:00
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* 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 {
return root
}
if root.Val < val {
root = root.Right
} else {
root = root.Left
}
}
return nil
}