2022-06-09 15:45:14 +01:00

30 lines
430 B
Go

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
}