'''
keys: 
Solutions:
Similar: 
T:
S:
'''
from typing import List
import collections

# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children


class Solution:
    # recursion; O(logN) for S
    def levelOrder1(self, root: 'Node') -> List[List[int]]:
        def traverse_node(node, level):
            if len(result) == level:
                result.append([])
            result[level].append(node.val)
            for child in node.children:
                traverse_node(child, level + 1)

        result = []
    
        if root is not None:
            traverse_node(root, 0)
        return result
    
    # bfs with queue
    # O(n) for S and T, n as the number of nodes
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []
        
        res = []
        queue = collections.deque([root])
        while queue:
            level = []
            for _ in range(len(queue)):
                node = queue.popleft()
                level.append(node.val)
                queue.extend(node.children) # one dimension queue
            res.append(level)
        return result
        