""" # Definition for a Node. class Node: def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): self.val = val self.left = left self.right = right self.next = next """
classSolution: defconnect(self, root: 'Node') -> 'Node': ifnot root: return root upper_quque = [root] lower_quque = [] while upper_quque: last_node = None for node in upper_quque: if last_node: last_node.next = node if node.left: lower_quque.append(node.left) if node.right: lower_quque.append(node.right) last_node = node upper_quque = lower_quque lower_quque = [] return root