LeetCode 429: N-ary Tree Level Order Traversal Solution

Master LeetCode problem 429 (N-ary Tree Level Order Traversal), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

429. N-ary Tree Level Order Traversal

Problem Explanation

Explanation

To perform a level order traversal of an n-ary tree, we can use a Breadth First Search (BFS) approach. We start by adding the root node to a queue. Then, we repeatedly dequeue a node, add its children to the queue, and add its value to the current level list. This process continues until the queue is empty. At each level, we store the values of the nodes in a separate list.

Algorithm:

  1. Initialize a queue to store the nodes.
  2. If the root is not null, add it to the queue.
  3. While the queue is not empty:
    • Initialize a list to store the values of nodes at the current level.
    • For each node in the queue (nodes at the current level):
      • Dequeue the node.
      • Add the value of the node to the current level list.
      • Add all children of the node to the queue.
    • Add the current level list to the result list.
  4. Return the result list containing the level order traversal.

Time Complexity:

  • Since we visit each node exactly once, the time complexity is O(n), where n is the number of nodes in the tree.

Space Complexity:

  • The space complexity is O(n), where n is the number of nodes in the tree, as we use a queue to store the nodes.

Solution Code

import java.util.*;

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        
        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            List<Integer> levelValues = new ArrayList<>();
            
            for (int i = 0; i < levelSize; i++) {
                Node node = queue.poll();
                levelValues.add(node.val);
                for (Node child : node.children) {
                    if (child != null) {
                        queue.offer(child);
                    }
                }
            }
            
            result.add(levelValues);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 429 (N-ary Tree Level Order Traversal)?

This page provides optimized solutions for LeetCode problem 429 (N-ary Tree Level Order Traversal) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 429 (N-ary Tree Level Order Traversal)?

The time complexity for LeetCode 429 (N-ary Tree Level Order Traversal) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 429 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 429 in Java, C++, or Python.

Back to LeetCode Solutions