MediumTreesFree practice
Binary Tree Level Order Traversal
Asked atamazongooglemicrosoftfacebookoracle
01 · Problem
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).
02 · Examples
Example 01
3
/ \
9 20
/ \
15 7Input
root = [3,9,20,null,null,15,7]
Output
[[3],[9,20],[15,7]]
Level 0 contains just the root [3]. Level 1 contains [9, 20]. Level 2 contains [15, 7] (children of 20).
Example 02
Input
root = [1]
Output
[[1]]
With a single-node tree, there is only one level containing the root value [1].
03 · Constraints
- 01The number of nodes in the tree is in the range [0, 2000].
- 02-1000 <= Node.val <= 1000
04 · Optimal complexity
- Time
- O(n)
- Space
- O(n)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.