Back to Practice Dashboard
Top 150 InterviewMedium

Binary Tree Level Order Traversal

Learn how to solve the 'Binary Tree Level Order Traversal' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Medium

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).

The tree is represented as a level-order list. Implement a function levelOrder(root: list) -> list that returns a list of lists, where each inner list contains the values at that level.

Constraints
  • The number of nodes in the tree is in the range [0, 2000]
  • -1000 <= Node.val <= 1000

Examples

Example 1
Input
[3,9,20,None,None,15,7]
Output
[[3],[9,20],[15,7]]
Explanation

Level 0: [3]. Level 1: [9,20]. Level 2: [15,7].

Example 2
Input
[1]
Output
[[1]]
Explanation

Only one node at level 0.

Example 3
Input
[]
Output
[]
Explanation

Empty tree has no levels.

Need a Hint?
Perform a recursive tree traversal (DFS) or level-order traversal (BFS) using a queue/stack.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor