forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreadth First Search
29 lines (28 loc) · 1.35 KB
/
Breadth First Search
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
/*
1) Queues - nodes in each level
2) Level counter - level of tree
3) Size counter - number of nodes in each level
*/
// Sample program to find the minimum depth of a tree
public int minDepth(TreeNode root) {
if (root == null)
return 0;
Queue<TreeNode> queue = new LinkedList();
int level = 1; // Root has level 1
queue.add(root); // Add root to queue
while (!queue.isEmpty()) { // Loop until reaches last level
int size = queue.size();
while (size > 0) { // Loop through each nodes for one level
TreeNode node = queue.poll(); // Expand for each node in each level
if (node.left == null && node.right == null)
return level;
if (node.left != null) // Left subtree of each node
queue.add(node.left);
if (node.right != null) // Right subtree of each node
queue.add(node.right);
size--;
}
level++; // Moving to next level
}
return level;
}