0% completed
Problem Statement
Given an n-ary tree, return a list representing the level order traversal of the nodes' values in this tree.
The input tree is serialized in an array format using level order traversal, where the children of each node are grouped together and separated by a null
value.
Examples
Example 1
- Input: root =
[1, null, 2, 3, 4, null, 5, 6]
- Expected Output:
[[1], [2, 3, 4], [5, 6]]
- Justification: The root node
1
is at level 0. Nodes2
,3
, and4
are the children of1
and are at level 1. Nodes5
and6
are the children of2
, and at level 3.
Example 2
- Input: root =
[7, null, 3, 8, 5, null, 2, 9, null, 6, null, 1, 4, 10]
- Expected Output:
[[7], [3, 8, 5], [2, 9, 6, 1, 4, 10]]
- Justification: The root node
7
is at level 0. Nodes3
,8
, and5
are its children at level 1. Nodes2
,9
,6
,1
,4
, and10
are at level 2.
Example 3
- Input: root =
[10, null, 15, 12, null, 20, null, 25, null, 30, 40]
- Expected Output:
[[10], [15, 12], [20, 25], [30, 40]]
- Justification: The root node
10
is at level 0. Nodes15
and12
are its children at level 1. Node20
and25
are at level 2. Nodes30
, and40
are at level 3.
Constraints:
- The height of the n-ary tree is less than or equal to 1000
- The total number of nodes is between [0, 10<sup>4</sup>]
Solution
To solve this problem, we use a queue-based approach to perform a level order traversal of the n-ary tree. The main idea is to process all nodes level by level, starting from the root. We use a queue to keep track of nodes at each level. At each step, we remove nodes from the queue, record their values, and add their children to the queue. This ensures that we traverse all nodes at the current level before moving to the next.
This approach is effective because it processes each node exactly once, which makes it efficient with a time complexity of O(N), where N is the total number of nodes in the tree. The use of a queue helps manage nodes level by level, ensuring that the output is correctly grouped. By storing nodes of each level in a separate list, we maintain the order of traversal and construct the desired output format.
Step-by-Step Algorithm
-
Initialize Result and Queue:
- Create an empty list
result
to store the final output. - If
root
isnull
, return the emptyresult
. - Initialize a queue and add the
root
node to it.
- Create an empty list
-
Level Order Traversal:
- While the queue is not empty:
- Determine the number of nodes at the current level (
size
). - Create an empty list
level
for the current level values. - For each node at the current level:
- Remove the node from the queue and add its value to
level
. - Add all children of the node to the queue.
- Remove the node from the queue and add its value to
- Add
level
to theresult
.
- Determine the number of nodes at the current level (
- While the queue is not empty:
-
Return Result:
- Return the
result
containing all level values.
- Return the
Algorithm Walkthrough
For the input root = [1, null, 2, 3, 4, null, 5, 6]
:
-
Initialize:
Start by creating an empty listresult = []
to store the output and a queue initialized with the root node:queue = [1]
. -
First Level:
- The size of the current level is
size = 1
(only the root node). - Create an empty list for this level:
level = []
. - Dequeue the first node,
1
, from the queue, and add its value tolevel
:level = [1]
. - Enqueue its children,
2, 3, 4
, into the queue:queue = [2, 3, 4]
. - Add the
level
list to the result:result = [[1]]
.
- The size of the current level is
-
Second Level:
- The size of the current level is
size = 3
(nodes2, 3, 4
). - Create an empty list for this level:
level = []
. - Dequeue node
2
from the queue, add its value tolevel
:level = [2]
, and enqueue its children5, 6
:queue = [3, 4, 5, 6]
. - Dequeue node
3
, add its value tolevel
:level = [2, 3]
(node3
has no children). - Dequeue node
4
, add its value tolevel
:level = [2, 3, 4]
(node4
has no children). - Add the
level
list to the result:result = [[1], [2, 3, 4]]
.
- The size of the current level is
-
Third Level:
- The size of the current level is
size = 2
(nodes5, 6
). - Create an empty list for this level:
level = []
. - Dequeue node
5
from the queue, add its value tolevel
:level = [5]
(node5
has no children). - Dequeue node
6
, add its value tolevel
:level = [5, 6]
(node6
has no children). - Add the
level
list to the result:result = [[1], [2, 3, 4], [5, 6]]
.
- The size of the current level is
-
Completion:
- The queue is now empty, which means all levels have been processed.
- Return the
result
:[[1], [2, 3, 4], [5, 6]]
.
Code
Complexity Analysis
Time Complexity
- The algorithm traverses each node in the n-ary tree exactly once.
- For each node, the algorithm processes its children, which involves adding them to the queue.
- Thus, the total time complexity is O(N), where
N
is the total number of nodes in the tree.
Space Complexity
- The queue may store up to O(W) nodes at the widest level of the tree, where W is the maximum width of the tree.
- Therefore, the space complexity is O(N) in the worst case, which occurs when the tree is a balanced tree or has a large number of nodes at the same level.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible