Files
LeetCode-Go/website/content/ChapterFour/0111.Minimum-Depth-of-Binary-Tree.md
2020-08-09 00:39:24 +08:00

62 lines
1.2 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)
## 题目
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note**: A leaf is a node with no children.
**Example**:
Given binary tree `[3,9,20,null,null,15,7]`,
3
/ \
9 20
/ \
15 7
return its minimum depth = 2.
## 题目大意
给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。
## 解题思路
- 递归求出根节点到叶子节点的深度,输出最小值即可
## 代码
```go
package leetcode
/**
* 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
}
```