‹ DS&A interview · Socratic
DSA · Trees · #43

Binary Tree Level Order Traversal

Module 44 · difficulty 3/5·30:00starts on first keystroke

Given the `root` of a binary tree, return the level order traversal of its nodes' values — i.e. values grouped from left to right, level by level. Implement `levelOrder(root)` where each tree node is an object `{ val, left, right }` (with `left`/`right` being a child node or `null`). Return an array of arrays: the i-th inner array holds the values at depth `i` (root is depth 0), read left to right.

Examples
  • root = [3,9,20,null,null,15,7] [[3],[9,20],[15,7]]Level 0 is [3], level 1 is [9,20], level 2 is [15,7].
  • root = [1] [[1]]
  • root = [] []An empty tree yields an empty list.
Constraints
  • · The number of nodes in the tree is in the range [0, 2000].
  • · -1000 <= Node.val <= 1000
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.