Binary Tree Level Order Traversal⚓︎
Description⚓︎
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).
Example 1:
- Input:
root = [3,9,20,null,null,15,7]
- Output:
[[3],[9,20],[15,7]]
Example 2:
- Input:
root = [1]
- Output:
[[1]]
Example 3:
- Input:
root = []
- Output:
[]
Constraints:
- The number of nodes in the tree is in the range
[0, 2000]
. -1000 <= Node.val <= 1000
Solution⚓︎
Way 1 (Not Recommended)⚓︎
Way 2 (Recommended)⚓︎
Code with comments:
Algorithm Analysis:
- Initialize Data Structures:
- Create an empty queue (
q
) that will be used to store the nodes of the tree at each level. - Create an empty list (
res
) that will eventually hold the level order traversal result. This list will contain sublists, each representing one level of the tree.
- Create an empty queue (
- Start with the Root Node:
- If the root node of the tree is not
nullptr
, add it to the queue. This step initiates the process of level order traversal.
- If the root node of the tree is not
- Traverse the Tree Level by Level:
- While the queue is not empty, perform the following steps. Each iteration of this loop corresponds to processing one level of the tree:
- Determine the number of nodes at the current level. This is given by the current size of the queue (
int len = q.size()
). - Create an empty list (
level
) that will store the values of the nodes at this current level.
- Determine the number of nodes at the current level. This is given by the current size of the queue (
- While the queue is not empty, perform the following steps. Each iteration of this loop corresponds to processing one level of the tree:
- Process Nodes at the Current Level:
- Repeat the following steps
len
times (the number of nodes at the current level):- Remove the front node from the queue and refer to it as
t
. - Add the value of
t
to thelevel
list. - If
t
has a left child, add the left child to the queue. - If
t
has a right child, add the right child to the queue.
- Remove the front node from the queue and refer to it as
- After processing all nodes at the current level, add the
level
list to theres
list.
- Repeat the following steps
- Repeat for All Levels:
- The while loop continues until the queue is empty. When the queue is empty, it means that all levels of the tree have been processed.
- Return the Result:
- After the while loop completes,
res
contains the level order traversal of the tree, and is returned as the result.
- After the while loop completes,
Complexity Analysis:
- Time Complexity: The time complexity of this algorithm is \(O(N)\), where \(N\) is the number of nodes in the tree. This is because every node in the tree is processed exactly once.
- Space Complexity: The space complexity of this algorithm is \(O(N)\) as well, in the worst case when the tree is completely unbalanced (e.g., every node has only one child), the queue will hold all nodes. For a balanced tree, the maximum size of the queue will be proportional to the width of the tree, which can be \(O(N)\) in the worst case.