Skip to content

Latest commit

 

History

History
24 lines (24 loc) · 677 Bytes

117.md

File metadata and controls

24 lines (24 loc) · 677 Bytes
"""
# 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
"""
class Solution:
    def connect(self, root: 'Node') -> 'Node':
        while root:
            cur = tmp = Node(0)
            while root:
                if root.left:
                    cur.next = root.left
                    cur = root.left
                if root.right:
                    cur.next = root.right
                    cur = root.right
                root = root.next
            root = tmp.next