版权声明:原创勿转 https://cloud.tencent.com/developer/article/1419469
根据BST的性质,递归
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return nil
}
if root.Val == val {
return root
}
if root.Val > val {
return searchBST(root.Left, val)
} else {
return searchBST(root.Right, val)
}
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode