BST最值
一句话说明
BST 的最小值一路向左走,最大值一路向右走。
Go 代码
func findMin(root *TreeNode) int {
if root == nil {
return 0
}
for root.Left != nil {
root = root.Left
}
return root.Val
}
func findMax(root *TreeNode) int {
if root == nil {
return 0
}
for root.Right != nil {
root = root.Right
}
return root.Val
}关键点
BST 已经帮你把最小值和最大值锁定在最边缘的链上了,不需要整棵树一起看。