Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39ea331

Browse files
committed
commit solution 111
1 parent ced7c6a commit 39ea331

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [111. 二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/)
22

33
### 题目描述
44

5+
<p>给定一个二叉树,找出其最小深度。</p>
6+
7+
<p>最小深度是从根节点到最近叶子节点的最短路径上的节点数量。</p>
8+
9+
<p><strong>说明:</strong>&nbsp;叶子节点是指没有子节点的节点。</p>
10+
11+
<p><strong>示例:</strong></p>
12+
13+
<p>给定二叉树&nbsp;<code>[3,9,20,null,null,15,7]</code>,</p>
14+
15+
<pre> 3
16+
/ \
17+
9 20
18+
/ \
19+
15 7</pre>
20+
21+
<p>返回它的最小深度 &nbsp;2.</p>
522

623
### 解题思路
724

825

926
### 具体解法
1027

11-
<!-- tabs:start -->
28+
1. BFS
1229

1330
#### **Golang**
1431
```go
15-
32+
func minDepth(root *TreeNode) int {
33+
if root == nil {
34+
return 0
35+
}
36+
count := 1
37+
queue := []*TreeNode{root}
38+
for len(queue) > 0 {
39+
l := len(queue)
40+
for i := 0; i < l; i++ {
41+
node := queue[i]
42+
if node.Left == nil && node.Right == nil {
43+
return count
44+
}
45+
if node.Left != nil {
46+
queue = append(queue, node.Left)
47+
}
48+
if node.Right != nil {
49+
queue = append(queue, node.Right)
50+
}
51+
}
52+
count++
53+
queue = queue[l:]
54+
}
55+
return count
56+
}
1657
```
1758

18-
<!-- tabs:end -->
1959

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=111 lang=golang
5+
*
6+
* [111] 二叉树的最小深度
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
type TreeNode struct {
20+
Val int
21+
Left *TreeNode
22+
Right *TreeNode
23+
}
24+
25+
func minDepth(root *TreeNode) int {
26+
if root == nil {
27+
return 0
28+
}
29+
count := 1
30+
queue := []*TreeNode{root}
31+
for len(queue) > 0 {
32+
l := len(queue)
33+
for i := 0; i < l; i++ {
34+
node := queue[i]
35+
if node.Left == nil && node.Right == nil {
36+
return count
37+
}
38+
if node.Left != nil {
39+
queue = append(queue, node.Left)
40+
}
41+
if node.Right != nil {
42+
queue = append(queue, node.Right)
43+
}
44+
}
45+
count++
46+
queue = queue[l:]
47+
}
48+
return count
49+
}
50+
51+
// @lc code=end

‎solution/200-299/0242.valid-anagram/solution_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@ import (
44
"testing"
55
)
66

7-
func TestHammingWeight(t *testing.T) {
8-
var num uint32
9-
var ret int
7+
func TestIsAnagram(t *testing.T) {
8+
var ss string
9+
var ts string
10+
var ret bool
1011

11-
num = 00000000000000000000000000001011
12-
ret = 3
12+
ss = "dmomkifm"
13+
ts = "skmokj"
14+
ret = false
1315

14-
if ret != hammingWeight(num) {
16+
if ret != isAnagram(ss, ts) {
17+
t.Fatalf("case fails %v\n", ret)
18+
}
19+
20+
ss = "ddss"
21+
ts = "ssdd"
22+
ret = true
23+
24+
if ret != isAnagram(ss, ts) {
25+
t.Fatalf("case fails %v\n", ret)
26+
}
27+
28+
ss = "ddsse"
29+
ts = "ssddd"
30+
ret = false
31+
32+
if ret != isAnagram(ss, ts) {
1533
t.Fatalf("case fails %v\n", ret)
1634
}
1735
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /