Algorithm-Day42-Binary-Tree-Level-Order-Traversal-lc-102
π§© Problem Description
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).
π¬ Example
1 | Input: root = [3,9,20,null,null,15,7] |
π‘ Approach: Breadth-First Search (BFS)
This is a classic BFS traversal problem using a queue:
- Start from the root and process each level of the tree.
- For each node in the current level, push its children into the queue.
- Collect all node values level by level.
π’ Java Code
1 | class Solution { |
β± Time and Space Complexity
- Time Complexity: O(n) β every node is visited once
- Space Complexity: O(n) β queue + output list
βοΈ Summary
- Straightforward BFS template.
- Common interview question to test level-by-level traversal.
- Often used in problems involving:
- Tree depth
- Zigzag/spiral level order
- Node connect (next pointers)
Try implementing variations like
lc-107
,lc-103
,lc-199
.