mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
38 lines
587 B
Go
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
|
|
}
|