429. N-ary Tree Level Order Traversal LeetCode Solution

In this guide, you will get 429. N-ary Tree Level Order Traversal LeetCode Solution with the best time and space complexity. The solution to N-ary Tree Level Order Traversal problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. N-ary Tree Level Order Traversal solution in C++
  4. N-ary Tree Level Order Traversal solution in Java
  5. N-ary Tree Level Order Traversal solution in Python
  6. Additional Resources
429. N-ary Tree Level Order Traversal LeetCode Solution image

Problem Statement of N-ary Tree Level Order Traversal

Given an n-ary tree, return the level order traversal of its nodes’ values.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]

Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]

Constraints:

The height of the n-ary tree is less than or equal to 1000
The total number of nodes is between [0, 104]

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

429. N-ary Tree Level Order Traversal LeetCode Solution in C++

class Solution {
 public:
  vector<vector<int>> levelOrder(Node* root) {
    if (root == nullptr)
      return {};

    vector<vector<int>> ans;
    queue<Node*> q{{root}};

    while (!q.empty()) {
      vector<int> currLevel;
      for (int sz = q.size(); sz > 0; --sz) {
        Node* node = q.front();
        q.pop();
        currLevel.push_back(node->val);
        for (Node* child : node->children)
          q.push(child);
      }
      ans.push_back(currLevel);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

429. N-ary Tree Level Order Traversal LeetCode Solution in Java

class Solution {
  public List<List<Integer>> levelOrder(Node root) {
    if (root == null)
      return new ArrayList<>();

    List<List<Integer>> ans = new ArrayList<>();
    Queue<Node> q = new ArrayDeque<>(List.of(root));

    while (!q.isEmpty()) {
      List<Integer> currLevel = new ArrayList<>();
      for (int sz = q.size(); sz > 0; --sz) {
        Node node = q.poll();
        currLevel.add(node.val);
        for (Node child : node.children)
          q.offer(child);
      }
      ans.add(currLevel);
    }

    return ans;
  }
}
// code provided by PROGIEZ

429. N-ary Tree Level Order Traversal LeetCode Solution in Python

class Solution:
  def levelOrder(self, root: 'Node') -> list[list[int]]:
    if not root:
      return []

    ans = []
    q = collections.deque([root])

    while q:
      currLevel = []
      for _ in range(len(q)):
        node = q.popleft()
        currLevel.append(node.val)
        for child in node.children:
          q.append(child)
      ans.append(currLevel)

    return ans
# code by PROGIEZ

Additional Resources

See also  389. Find the Difference LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.