Problems › Traversals

Level-Order Traversal

Difficulty: Medium · Topic: Traversals

Return the level-order traversal as a list of levels — each inner list holds the node values at one depth, top to bottom, left to right. Use a queue (breadth-first). The build_tree / TreeNode helper is provided.

Examples

level_order([3,9,20,null,null,15,7])
→ [[3],[9,20],[15,7]]
level_order([1])
→ [[1]]

Target complexity: time O(n), space O(n)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Traversals problems