Python easy to understand solution (similar to level order traversal).


  • 0
    C
    def connect(self, root):
        if not root:
            return 
        # connect nodes level by level,
        # similar to level order traversal
        queue = collections.deque([root])
        nextLevel = collections.deque([])
        while queue:
            node = queue.popleft()
            if node.left:
                nextLevel.append(node.left)
            if node.right:
                nextLevel.append(node.right)
            if queue:
                node.next = queue[0]
            if not queue:
                queue, nextLevel = nextLevel, queue

  • 0
    C

    Unfortunately, this is O(n) not O(1) space


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.