Skip to content

Commit 2c240e5

Browse files
committed
MinimumDepthOfBinaryTree111
1 parent de26b06 commit 2c240e5

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040

4141

4242

43-
# Total: 238
43+
# Total: 239
4444

4545
| Easy | Medium | Hard | - |
4646
|:----:|:-------:|:----:|:-:|
47-
| 58 | 137 | 41 | 2 |
47+
| 59 | 137 | 41 | 2 |
4848

4949

5050
| Question | Solution | Difficulty |
@@ -130,6 +130,7 @@
130130
| [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConvertSortedArrayToBinarySearchTree108.java) | Easy |
131131
| [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ConvertSortedListToBST109.java) | Medium |
132132
| [110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BalancedBinaryTree110.java) | Easy |
133+
| [111. Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MinimumDepthOfBinaryTree111.java) | Easy |
133134
| [112. Path Sum](https://leetcode.com/problems/path-sum/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/PathSum112.java) | Easy |
134135
| [114. Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FlattenBinaryTreeToLinkedList114.java) | Medium |
135136
| [116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/PopulatingNextRightPointersInEachNode116.java) | Medium |

src/MinimumDepthOfBinaryTree111.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Given a binary tree, find its minimum depth.
3+
*
4+
* The minimum depth is the number of nodes along the shortest path from the
5+
* root node down to the nearest leaf node.
6+
*
7+
* Note: A leaf is a node with no children.
8+
*
9+
* Example:
10+
*
11+
* Given binary tree [3,9,20,null,null,15,7],
12+
*
13+
* 3
14+
* / \
15+
* 9 20
16+
* / \
17+
* 15 7
18+
* return its minimum depth = 2.
19+
*
20+
*/
21+
22+
/**
23+
* Definition for a binary tree node.
24+
* public class TreeNode {
25+
* int val;
26+
* TreeNode left;
27+
* TreeNode right;
28+
* TreeNode(int x) { val = x; }
29+
* }
30+
*/
31+
32+
public class MinimumDepthOfBinaryTree111 {
33+
public int minDepth(TreeNode root) {
34+
if (root == null) return 0;
35+
Queue<TreeNode> q = new LinkedList<>();
36+
int depth = 1;
37+
q.add(root);
38+
while (!q.isEmpty()) {
39+
int size = q.size();
40+
for (int i=0; i<size; i++) {
41+
TreeNode node = q.remove();
42+
boolean isLeaf = true;
43+
if (node.left != null) {
44+
isLeaf = false;
45+
q.add(node.left);
46+
}
47+
if (node.right != null) {
48+
isLeaf = false;
49+
q.add(node.right);
50+
}
51+
if (isLeaf) return depth;
52+
}
53+
depth++;
54+
}
55+
return depth;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)