30 lines
430 B
Go
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
|
|
}
|