|
| 1 | +[](https://github.com/javadev/LeetCode-in-All) |
| 2 | +[](https://github.com/javadev/LeetCode-in-All/fork) |
| 3 | + |
| 4 | +## 102\. Binary Tree Level Order Traversal |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level). |
| 9 | + |
| 10 | +**Example 1:** |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +**Input:** root = [3,9,20,null,null,15,7] |
| 15 | + |
| 16 | +**Output:** [[3],[9,20],[15,7]] |
| 17 | + |
| 18 | +**Example 2:** |
| 19 | + |
| 20 | +**Input:** root = [1] |
| 21 | + |
| 22 | +**Output:** [[1]] |
| 23 | + |
| 24 | +**Example 3:** |
| 25 | + |
| 26 | +**Input:** root = [] |
| 27 | + |
| 28 | +**Output:** [] |
| 29 | + |
| 30 | +**Constraints:** |
| 31 | + |
| 32 | +* The number of nodes in the tree is in the range `[0, 2000]`. |
| 33 | +* `-1000 <= Node.val <= 1000` |
| 34 | + |
| 35 | +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: |
| 36 | + |
| 37 | +1. **Create a `Solution` class**: Define a class named `Solution` to encapsulate our solution methods. |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +3. **Initialize a queue**: Create a queue to store the nodes during BFS traversal. |
| 42 | + |
| 43 | +4. **Check for null root**: Check if the root is null. If it is, return an empty list. |
| 44 | + |
| 45 | +5. **Perform BFS traversal**: Enqueue the root node into the queue. While the queue is not empty: |
| 46 | + - Dequeue the front node from the queue. |
| 47 | + - Add the value of the dequeued node to the current level list. |
| 48 | + - Enqueue the left and right children of the dequeued node if they exist. |
| 49 | + - Move to the next level when all nodes in the current level are processed. |
| 50 | + |
| 51 | +6. **Return the result**: After the BFS traversal is complete, return the list containing the level order traversal of the binary tree. |
| 52 | + |
| 53 | +Here's the Java implementation: |
| 54 | + |
| 55 | +```java |
| 56 | +import java.util.ArrayList; |
| 57 | +import java.util.LinkedList; |
| 58 | +import java.util.List; |
| 59 | +import java.util.Queue; |
| 60 | + |
| 61 | +class Solution { |
| 62 | + public List<List<Integer>> levelOrder(TreeNode root) { |
| 63 | + List<List<Integer>> result = new ArrayList<>(); // Initialize list to store level order traversal |
| 64 | + if (root == null) return result; // Check for empty tree |
| 65 | + |
| 66 | + Queue<TreeNode> queue = new LinkedList<>(); // Initialize queue for BFS traversal |
| 67 | + queue.offer(root); // Enqueue the root node |
| 68 | + |
| 69 | + while (!queue.isEmpty()) { |
| 70 | + int levelSize = queue.size(); // Get the number of nodes in the current level |
| 71 | + List<Integer> level = new ArrayList<>(); // Initialize list for the current level |
| 72 | + |
| 73 | + for (int i = 0; i < levelSize; i++) { |
| 74 | + TreeNode node = queue.poll(); // Dequeue the front node |
| 75 | + level.add(node.val); // Add node value to the current level list |
| 76 | + |
| 77 | + // Enqueue the left and right children if they exist |
| 78 | + if (node.left != null) queue.offer(node.left); |
| 79 | + if (node.right != null) queue.offer(node.right); |
| 80 | + } |
| 81 | + |
| 82 | + result.add(level); // Add the current level list to the result list |
| 83 | + } |
| 84 | + |
| 85 | + return result; // Return the level order traversal |
| 86 | + } |
| 87 | + |
| 88 | + // Definition for a TreeNode |
| 89 | + public class TreeNode { |
| 90 | + int val; |
| 91 | + TreeNode left; |
| 92 | + TreeNode right; |
| 93 | + |
| 94 | + TreeNode() {} |
| 95 | + TreeNode(int val) { this.val = val; } |
| 96 | + TreeNode(int val, TreeNode left, TreeNode right) { |
| 97 | + this.val = val; |
| 98 | + this.left = left; |
| 99 | + this.right = right; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +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