/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findSecondMinimumValue(root *TreeNode) int {
// 第二小就是左右子树的最小节点,因为根节点必然最小
if root == nil {
return -1
}
return helper(root, root.Val)
}
func helper(node *TreeNode, min int) int {
if node == nil {
return -1
}
if node.Val > min {
return node.Val
}
left, right := helper(node.Left, node.Val), helper(node.Right, node.Val)
if left == -1 {
return right
}
if right == -1 {
return left
}
if left > right {
return right
}
return left
}