Binary Tree Level Order Traversal
Medium · Trees · Tree · BFS · Queue
Problem
Return the values of a binary tree level by level, top to bottom, each level left to right as its own list.
from_level() and to_level() are provided.
Examples
Input: root = [3,9,20,None,None,15,7] Output: [[3],[9,20],[15,7]]
Three levels.
Input: root = [] Output: []
Constraints
- 0 <= number of nodes <= 2000
- -1000 <= Node.val <= 1000
Approaches
- DFS carrying the depth — time O(n), space O(height)
Related problems
Start free on Cariara · Coding problems · System design studio