Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode N-ary Tree Level Order Traversal problem solution

YASH PAL, 31 July 2024

In this Leetcode N-ary Tree Level Order Traversal problem solution we have given an n-ary tree, returns 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.

Leetcode N-ary Tree Level Order Traversal problem solution

Problem solution in Python.

class Solution(object):
    def levelOrder(self, root):
        if not root:return []
        res = []
        stack = [root]
        while stack:
            temp = []
            next_stack = []
            for node in stack:
                temp.append(node.val)
                for child in node.children:
                    next_stack.append(child)
            stack = next_stack
            res.append(temp)
        return res

Problem solution in Java.

class Solution {
public List<List> levelOrder(Node root) {

    Queue <Node> q1=new LinkedList();
    Queue  <Node> q2=new LinkedList();
    List<List<Integer>> list=new LinkedList<List<Integer>>();
    if(root==null){
        return list;
    }
    List<Integer> list1=new ArrayList<>();
    q1.add(root);
    while(q1.size()!=0){
        Node a =q1.remove();
         // System.out.print(a.val+" ");
        list1.add(a.val);
        for(Node child:a.children){
            q2.add(child);
        }
        if(q1.size()==0){
            q1=q2;
            q2=new LinkedList();
            list.add(list1);
            list1=new ArrayList<>();
            // System.out.println();
        }
        
        
    }
    return list;
}
}

Problem solution in C++.

class Solution {
public:
    
    vector<vector<int>> levelOrder(Node* root) 
    {
        vector<vector<int>> ret;
        if(root == nullptr) return ret;
        
        queue<Node*> s;
        queue<int> num;
        
        s.push(root);
        num.push(1);
        
        while(!s.empty()) {
            
            int N = num.front();
            num.pop();
            
            int Nnext = 0;
            ret.push_back(vector<int>());
            
            for(int n=0; n<N; ++n) {
                Node *node = s.front();
                s.pop();
                Nnext += node->children.size();
                ret.back().push_back(node->val);
                for(int k=0; k<node->children.size(); ++k) s.push(node->children[k]);
            }
            
            num.push(Nnext);
        }
        
        return ret;
        
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes