Skip to content

Commit f888272

Browse files
committed
二叉树的最小深度
1 parent c7b9be0 commit f888272

File tree

10 files changed

+148
-1
lines changed

10 files changed

+148
-1
lines changed

LeetCode/Doc/doc-sample.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414
## 代码
1515

16-
[这里](../Code/n.py)
16+
[这里](../src/n/Solution.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Java中常见数据结构的使用方法
2+
3+
## 队列
4+
```java
5+
Queue<String> queue = new LinkedList<String>();
6+
//添加元素
7+
queue.offer("a");
8+
//返回第一个元素,并在队列中删除
9+
String first=queue.poll();
10+
queue.element(); //返回第一个元素
11+
queue.peek(); //返回第一个元素
12+
13+
```
14+
##
15+
16+
Stack<Integer\> st = new Stack<Integer\>();
17+
18+
| 序号 | 方法描述 |
19+
| :--- | :----------------------------------------------------------- |
20+
| 1 | boolean empty() 测试堆栈是否为空。 |
21+
| 2 | Object peek( ) 查看堆栈顶部的对象,但不从堆栈中移除它。 |
22+
| 3 | Object pop( ) 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
23+
| 4 | Object push(Object element) 把项压入堆栈顶部。 |
24+
| 5 | int search(Object element) 返回对象在堆栈中的位置,以 1 为基数。 |
25+
26+
27+
28+
29+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 二叉树的最小深度(minimum-depth-of-binary-tree)
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
8+
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.
9+
10+
给定一个二叉树,找出其最小的深度。最小的深度指的是从根节点到距离其最近的叶子节点之间路径中的节点数。
11+
12+
## 解题思路
13+
14+
利用层序遍历的思想,数据结构采用队列,从第一层开始在每一层的最后一个节点入队之后再多入一个null,标记当前层已经结束,这样每当出队的元素为null,则深度就可加一,附加判断当前节点是否是叶子节点即可。
15+
16+
## 代码
17+
18+
[这里](../src/one/Solution.java)

LeetCode/LeetCode.iml

+10
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
1020
</component>
1121
</module>
Binary file not shown.
350 Bytes
Binary file not shown.
Binary file not shown.

LeetCode/src/one/Solution.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package one;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* @author dmrfcoder
8+
* @date 2019/4/10
9+
*/
10+
class TreeNode {
11+
int val;
12+
TreeNode left;
13+
TreeNode right;
14+
15+
TreeNode(int x) {
16+
val = x;
17+
}
18+
}
19+
20+
public class Solution {
21+
public int run(TreeNode root) {
22+
if (root == null) {
23+
return 0;
24+
}
25+
26+
Queue<TreeNode> treeNodeQueue = new LinkedList<TreeNode>();
27+
treeNodeQueue.offer(root);
28+
treeNodeQueue.offer(null);
29+
30+
31+
int minDepth = 1;
32+
33+
while (!treeNodeQueue.isEmpty()) {
34+
TreeNode curNode = treeNodeQueue.poll();
35+
if (curNode == null) {
36+
37+
if (treeNodeQueue.isEmpty()) {
38+
return minDepth;
39+
} else {
40+
minDepth += 1;
41+
treeNodeQueue.offer(null);
42+
}
43+
} else {
44+
if (curNode.left == null && curNode.right == null) {
45+
return minDepth;
46+
}
47+
if (curNode.left != null) {
48+
treeNodeQueue.offer(curNode.left);
49+
}
50+
if (curNode.right != null) {
51+
treeNodeQueue.offer(curNode.right);
52+
}
53+
}
54+
55+
56+
}
57+
return minDepth;
58+
}
59+
60+
61+
62+
63+
public static void main(String[] args) {
64+
Solution solution = new Solution();
65+
TreeNode root = new TreeNode(1);
66+
TreeNode node2 = new TreeNode(2);
67+
TreeNode node3 = new TreeNode(2);
68+
TreeNode node4 = new TreeNode(2);
69+
TreeNode node5 = new TreeNode(2);
70+
root.left = node2;
71+
root.right = node3;
72+
node2.left = node4;
73+
node2.right = node5;
74+
75+
System.out.println(solution.run(root));
76+
77+
}
78+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package samplepackage;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019/4/10
6+
*/
7+
public class Solution {
8+
}

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,7 @@
140140

141141

142142

143+
# LeetCode
144+
145+
- [求解二叉树的最小深度](./LeetCode/Doc/二叉树的最小深度.md)
146+

0 commit comments

Comments
 (0)