1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @Title: 二叉树中第二小的节点 (Second Minimum Node In a Binary Tree)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 0 ms
// @Memory: 1.9 MB
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

 func findSecondMinimumValue(root *TreeNode) int {
	max := 1<<63 - 1
	hi := max
	var helper func(*TreeNode, int)
	helper = func(node *TreeNode, lo int) {
		if node == nil {
			return
		}
		if lo < node.Val && node.Val < hi {
			hi = node.Val
		}
		helper(node.Left, lo)
		helper(node.Right, lo)
	}

	helper(root, root.Val)

	if max == hi {
		return -1
	}

	return hi
}