- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem111.java
More file actions
Latest commit
47 lines (41 loc) · 1.19 KB
/
Copy pathProblem111.java
File metadata and controls
47 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
importjava.util.LinkedList;
importjava.util.Queue;
classProblem111 {
publicstaticvoidmain(String[] args) {
Problem111p = newProblem111();
TreeNoderoot = newTreeNode(1);
//root.left = new TreeNode(2);
//root.right = new TreeNode(3);
//root.left.right = new TreeNode(4);
System.out.println(p.minDepth(root));
}
publicintminDepth(TreeNoderoot) {
intdepth = 0;
if (root == null) {
returndepth;
}
if (root.left == null && root.right == null) {
return1;
}
Queue<TreeNode> queue = newLinkedList<>();
queue.add(root);
intlevel = 1;
intcount = 0;
while(queue.size() > 0) {
count = queue.size();
while(count>0) {
count--;
TreeNodet = queue.poll();
if (t.left == null && t.right == null) returnlevel;
if (t.left != null) {
queue.add(t.left);
}
if (t.right != null) {
queue.add(t.right);
}
}
level++;
}
return0;
}
}