Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 82c1ea2

Browse files
edit 111
1 parent fb865a8 commit 82c1ea2

File tree

1 file changed

+40
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-38
lines changed

src/main/java/com/fishercoder/solutions/_111.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,50 @@
55
import java.util.LinkedList;
66
import java.util.Queue;
77

8-
/**Given a binary tree, find its minimum depth.
9-
10-
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.*/
8+
/**
9+
* 111. Minimum Depth of Binary Tree
10+
* Given a binary tree, find its minimum depth.
11+
* The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
12+
* */
1113

1214
public class _111 {
13-
/**We can solve this problem using both BFS and DFS:
14-
* DFS is to visit every single root to leaf path and return the shortest one.
15-
* BFS is to visit every level and return whenever we find the first leaf node.*/
16-
public int minDepth(TreeNode root) {
17-
if(root == null) return 0;
18-
int left = minDepth(root.left);
19-
int right = minDepth(root.right);
20-
if(left == 0) return right+1;
21-
if(right == 0) return left+1;
22-
return Math.min(left, right)+1;
23-
}
24-
25-
public static void main(String[] args){
26-
_111 test = new _111();
27-
TreeNode root = new TreeNode(1);
28-
root.left = new TreeNode(2);
29-
root.right = new TreeNode(3);
30-
System.out.println(test.minDepth(root));
15+
/**
16+
* We can solve this problem using both BFS and DFS:
17+
* DFS is to visit every single root to leaf path and return the shortest one.
18+
* BFS is to visit every level and return whenever we find the first leaf node.
19+
*/
20+
21+
public static class DFSSolution {
22+
23+
public int minDepth(TreeNode root) {
24+
if (root == null) return 0;
25+
int left = minDepth(root.left);
26+
int right = minDepth(root.right);
27+
if (left == 0) return right + 1;
28+
if (right == 0) return left + 1;
29+
return Math.min(left, right) + 1;
30+
}
31+
3132
}
32-
33-
34-
public int minDepth_BFS(TreeNode root) {
35-
if(root == null) return 0;
36-
Queue<TreeNode> q = new LinkedList();
37-
q.offer(root);
38-
int level = 0;
39-
while(!q.isEmpty()){
40-
level++;
41-
int size = q.size();
42-
for(int i = 0; i < size; i++){
43-
TreeNode curr = q.poll();
44-
if(curr.left != null) q.offer(curr.left);
45-
if(curr.right != null) q.offer(curr.right);
46-
if(curr.left == null && curr.right == null) return level;
33+
34+
public static class BFSSolution {
35+
36+
public int minDepth_BFS(TreeNode root) {
37+
if (root == null) return 0;
38+
Queue<TreeNode> q = new LinkedList();
39+
q.offer(root);
40+
int level = 0;
41+
while (!q.isEmpty()) {
42+
level++;
43+
int size = q.size();
44+
for (int i = 0; i < size; i++) {
45+
TreeNode curr = q.poll();
46+
if (curr.left != null) q.offer(curr.left);
47+
if (curr.right != null) q.offer(curr.right);
48+
if (curr.left == null && curr.right == null) return level;
49+
}
4750
}
51+
return level;
4852
}
49-
return level;
5053
}
51-
5254
}

0 commit comments

Comments
 (0)