dfs
取左右子树第二大的值进行比较
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findSecondMinimumValue(root *TreeNode) int {
val := dfs(root, root.Val)
return val
}
func dfs(root *TreeNode, minV int) int {
if root == nil {
return -1
}
if root.Val > minV {
return root.Val
}
left := dfs(root.Left, minV)
right := dfs(root.Right, minV)
if left == -1 {
return right
} else if right == -1 {
return left
} else if left < right {
return left
} else {
return right
}
}
参考
671. 二叉树中第二小的节点 - 力扣(Leetcode)
标签:right,return,671,dfs,二叉树,TreeNode,root,leetcode,left From: https://www.cnblogs.com/wudanyang/p/17035325.html