版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89331175
根据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
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句