mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-10 13:37:33 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question111 struct {
|
||||
para111
|
||||
ans111
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para111 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans111 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem111(t *testing.T) {
|
||||
|
||||
qs := []question111{
|
||||
|
||||
question111{
|
||||
para111{[]int{}},
|
||||
ans111{0},
|
||||
},
|
||||
|
||||
question111{
|
||||
para111{[]int{1}},
|
||||
ans111{1},
|
||||
},
|
||||
|
||||
question111{
|
||||
para111{[]int{3, 9, 20, structures.NULL, structures.NULL, 15, 7}},
|
||||
ans111{2},
|
||||
},
|
||||
|
||||
question111{
|
||||
para111{[]int{1, 2}},
|
||||
ans111{2},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 111------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans111, q.para111
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", minDepth(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
32
leetcode/0111.Minimum-Depth-of-Binary-Tree/README.md
Executable file
32
leetcode/0111.Minimum-Depth-of-Binary-Tree/README.md
Executable file
@ -0,0 +1,32 @@
|
||||
# [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.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 递归求出根节点到叶子节点的深度,输出最小值即可
|
||||
|
Reference in New Issue
Block a user