12345678910111213141516171819202122232425262728293031323334353637383940 |
- /**
- * 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(root *TreeNode, min int) int {
- if root.Left == nil {
- return -1
- }
- var l, r int
- if root.Left.Val == min {
- l = helper(root.Left, min)
- } else {
- l = root.Left.Val
- }
- if root.Right.Val == min {
- r = helper(root.Right, min)
- } else {
- r = root.Right.Val
- }
- switch {
- case l == -1:
- return r
- case r == -1:
- return l
- case r < l:
- return r
- }
- return l
- }
|