Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 12, 2023
1 parent faad7df commit 9bf9c58
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import "fmt"

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil || root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
}

func main() {
root := &TreeNode{Val: 4}
root.Left = &TreeNode{Val: 2}
root.Right = &TreeNode{Val: 7}
root.Left.Left = &TreeNode{Val: 1}
root.Left.Right = &TreeNode{Val: 3}

val := 2

fmt.Println(searchBST(root, val))
}

0 comments on commit 9bf9c58

Please sign in to comment.