39 / 75
Medium
Binary Tree Level Order Traversal
#39treebfsqueue
Use BFS with queue to traverse tree level by level, collecting nodes at each level.
Example:
Input:
root=[3,9,20,null,null,15,7]Output:
[[3],[9,20],[15,7]]Common Mistakes:
- Not tracking level size (mixing nodes from different levels)
- Using DFS when BFS is more natural for this problem
- Forgetting to check for null children before adding to queue
- Not initializing separate list for each level
Notes:
Classic BFS problem. Time O(n), Space O(n) for queue. Can also solve with DFS passing level as parameter.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use BFS with queue to traverse tree level by level, collecting nodes at each level.
Example:
Input:
root=[3,9,20,null,null,15,7]Output:
[[3],[9,20],[15,7]]Common Mistakes:
- Not tracking level size (mixing nodes from different levels)
- Using DFS when BFS is more natural for this problem
- Forgetting to check for null children before adding to queue
- Not initializing separate list for each level
Notes:
Classic BFS problem. Time O(n), Space O(n) for queue. Can also solve with DFS passing level as parameter.
39/75
Binary Tree Level Order Traversal