Golang Leetcode 700. Search in a Binary Search Tree.go

思路

根据BST的性质,递归

code

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

你可能感兴趣的:(leetcode-golang,golang,leetcode,算法)