Files
LeetCode-Go/leetcode/0111.Minimum-Depth-of-Binary-Tree/111. Minimum Depth of Binary Tree.go
2020-08-07 17:06:53 +08:00

38 lines
587 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil {
return minDepth(root.Right) + 1
}
if root.Right == nil {
return minDepth(root.Left) + 1
}
return min(minDepth(root.Left), minDepth(root.Right)) + 1
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}