1490. Clone N ary Tree (Medium) - TengnanYao/daily_leetcode GitHub Wiki

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children if children is not None else []
"""

class Solution:
    def cloneTree(self, root: 'Node') -> 'Node':
        # recursion
        if root:
            return Node(root.val, [self.cloneTree(child) for child in root.children])
        
        # bfs
        if not root:
            return None
        head = Node(root.val, [])
        arr = [(root, head)]
        while arr:
            temp = []
            for node1, node2 in arr:
                for node in node1.children:
                    node_new = Node(node.val, [])
                    node2.children.append(node_new)
                    temp.append((node, node_new))
            arr = temp
        return head