You're given the root of a binary tree, and your task is to return the level order traversal of its nodes' values. This means you need to visit all nodes level by level, from left to right.
Think of it like reading a family tree or an organizational chart - you start at the top (root), then read all nodes at the second level from left to right, then all nodes at the third level from left to right, and so on.
Example: For a tree with root 3, left child 9, and right child 20 (where 20 has children 15 and 7), the level order traversal would be [[3], [9, 20], [15, 7]].
The result should be a 2D array where each inner array represents one level of the tree.
Input & Output
Visualization
Time & Space Complexity
Visit each node exactly once during BFS traversal
Queue stores at most w nodes, where w is maximum width of tree
Constraints
- The number of nodes in tree is in the range [0, 2000]
- Node values are in the range [-1000, 1000]
- Follow up: Can you solve this both recursively and iteratively?