Skip to content

Commit ef047cc

Browse files
committed
二叉树的最小深度
1 parent 4ac2555 commit ef047cc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 105 | 树、深度优先搜索 | [从前序与中序遍历序列构造二叉树](src/main/java/algorithm/leetcode/Solution105.java) | 中等 |
6565
| 106 | 树、深度优先搜索 | [从中序与后序遍历序列构造二叉树](src/main/java/algorithm/leetcode/Solution106.java) | 中等 |
6666
| 107 | 树、广度优先搜索 | [二叉树的层次遍历 II](src/main/java/algorithm/leetcode/Solution107.java) | 简单 |
67+
| 111 | 树、广度优先搜索、深度优先搜索 | [二叉树的最小深度](src/main/java/algorithm/leetcode/Solution111.java) | 简单 |
6768
| 120 | 数组、动态规划 | [三角形最小路径和](src/main/java/algorithm/leetcode/Solution120.java) | 中等 |
6869
| 121 | 数组、动态规划 | [买卖股票的最佳时机](src/main/java/algorithm/leetcode/Solution121.java) | 简单 |
6970
| 122 | 贪心算法、数组 | [买卖股票的最佳时机 II](src/main/java/algorithm/leetcode/Solution122.java) | 简单 |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package algorithm.leetcode;
2+
3+
/**
4+
* @author: mayuan
5+
* @desc: 二叉树的最小深度
6+
* @date: 2019/03/07
7+
*/
8+
public class Solution111 {
9+
10+
public int minDepth(TreeNode root) {
11+
if (null == root) {
12+
return 0;
13+
}
14+
15+
int l = minDepth(root.left);
16+
int r = minDepth(root.right);
17+
18+
if (0 == l || 0 == r) {
19+
return l + r + 1;
20+
} else {
21+
return Math.min(l, r) + 1;
22+
}
23+
}
24+
25+
class TreeNode {
26+
int val;
27+
TreeNode left;
28+
TreeNode right;
29+
30+
TreeNode(int x) {
31+
val = x;
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)