Leetcode Note: Go - Second Minimum Node in a Binary Tree
Second Minimum Node In a Binary Tree - LeetCode
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/
- Go 言語で取り組んだメモ
所感
- binary tree から 2番目 に小さい値を int で return する
回答
Second Minimum Node In a Binary Tree - LeetCode
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/solution/
Golang, 0ms, recursive - LeetCode Discuss
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/295498/Golang-0ms-recursive
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findSecondMinimumValue(root *TreeNode) int {
// The root node always contains the minimum value
// (due to the special BST definition)
return helper(root, root.Val, -1)
}
func helper(node *TreeNode, min, second int) int {
if node.Left == nil {
// If the current node is a leaf node and it's value is not equal to minimum
// then check if it's less than the current second minimum value
if node.Val != min && (node.Val < second || second == -1) {
return node.Val
}
return second
}
// Since the current node has subnodes, get their second minimum values
// and return the one that's not equal to -1 or the least of them if both are not -1
l := helper(node.Left, min, second)
r := helper(node.Right, min, second)
if l == -1 {
return r
}
if r == -1 || l < r {
return l
}
return r
}