Skip to content

Commit 0bbac78

Browse files
committed
Updated task 102
1 parent cbb1ffe commit 0bbac78

File tree

1 file changed

+26
-27
lines changed
  • src/main/java/g0101_0200/s0102_binary_tree_level_order_traversal

1 file changed

+26
-27
lines changed

src/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md

+26-27
Original file line numberDiff line numberDiff line change
@@ -29,61 +29,60 @@ Given the `root` of a binary tree, return _the level order traversal of its node
2929
* The number of nodes in the tree is in the range `[0, 2000]`.
3030
* `-1000 <= Node.val <= 1000`
3131

32-
To solve this problem in Java with a `Solution` class, we'll use a breadth-first search (BFS) traversal approach. Below are the steps:
32+
To solve the "Binary Tree Level Order Traversal" problem in Java with a `Solution` class, we'll perform a breadth-first search (BFS) traversal of the binary tree. Below are the steps:
3333

3434
1. **Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods.
3535

3636
2. **Create a `levelOrder` method**: This method takes the root node of the binary tree as input and returns the level order traversal of its nodes' values.
3737

38-
3. **Check for null root**: Check if the root is null. If so, return an empty list since there are no nodes to traverse.
38+
3. **Initialize a queue**: Create a queue to store the nodes during BFS traversal.
3939

40-
4. **Initialize a queue**: We'll use a queue to perform BFS traversal. Add the root node to the queue.
40+
4. **Check for null root**: Check if the root is null. If it is, return an empty list.
4141

42-
5. **Traverse the tree level by level**:
43-
- Start a loop that continues until the queue is empty.
44-
- Inside the loop, for each level, get the size of the queue (number of nodes at the current level).
45-
- Create a list to store the node values at the current level.
46-
- Iterate through all nodes at the current level:
47-
- Poll a node from the queue.
48-
- Add its value to the list.
49-
- If the node has left or right children, add them to the queue.
50-
- After processing all nodes at the current level, add the list of node values to the result list.
42+
5. **Perform BFS traversal**: Enqueue the root node into the queue. While the queue is not empty:
43+
- Dequeue the front node from the queue.
44+
- Add the value of the dequeued node to the current level list.
45+
- Enqueue the left and right children of the dequeued node if they exist.
46+
- Move to the next level when all nodes in the current level are processed.
5147

52-
6. **Return the result**: After traversing the entire tree, return the result containing the level order traversal.
48+
6. **Return the result**: After the BFS traversal is complete, return the list containing the level order traversal of the binary tree.
5349

5450
Here's the Java implementation:
5551

5652
```java
57-
import java.util.*;
53+
import java.util.ArrayList;
54+
import java.util.LinkedList;
55+
import java.util.List;
56+
import java.util.Queue;
5857

5958
class Solution {
6059
public List<List<Integer>> levelOrder(TreeNode root) {
61-
List<List<Integer>> result = new ArrayList<>();
62-
60+
List<List<Integer>> result = new ArrayList<>(); // Initialize list to store level order traversal
6361
if (root == null) return result; // Check for empty tree
6462

65-
Queue<TreeNode> queue = new LinkedList<>();
66-
queue.offer(root); // Add root to the queue
63+
Queue<TreeNode> queue = new LinkedList<>(); // Initialize queue for BFS traversal
64+
queue.offer(root); // Enqueue the root node
6765

6866
while (!queue.isEmpty()) {
69-
int levelSize = queue.size(); // Get the number of nodes at the current level
70-
List<Integer> levelNodes = new ArrayList<>(); // List to store node values at current level
67+
int levelSize = queue.size(); // Get the number of nodes in the current level
68+
List<Integer> level = new ArrayList<>(); // Initialize list for the current level
7169

7270
for (int i = 0; i < levelSize; i++) {
73-
TreeNode node = queue.poll(); // Remove node from the queue
74-
levelNodes.add(node.val); // Add its value to the list
71+
TreeNode node = queue.poll(); // Dequeue the front node
72+
level.add(node.val); // Add node value to the current level list
7573

76-
// Add left and right children to the queue if they exist
74+
// Enqueue the left and right children if they exist
7775
if (node.left != null) queue.offer(node.left);
7876
if (node.right != null) queue.offer(node.right);
7977
}
80-
result.add(levelNodes); // Add list of node values at current level to result
78+
79+
result.add(level); // Add the current level list to the result list
8180
}
8281

83-
return result;
82+
return result; // Return the level order traversal
8483
}
8584

86-
// TreeNode definition
85+
// Definition for a TreeNode
8786
public class TreeNode {
8887
int val;
8988
TreeNode left;
@@ -100,4 +99,4 @@ class Solution {
10099
}
101100
```
102101

103-
This implementation follows the steps outlined above and efficiently solves the problem of binary tree level order traversal in Java.
102+
This implementation follows the steps outlined above and efficiently computes the level order traversal of the binary tree in Java using BFS.

0 commit comments

Comments
 (0)