catch-up
This commit is contained in:
29
search-in-a-binary-search-tree/sol.go
Normal file
29
search-in-a-binary-search-tree/sol.go
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user