AC Python O(1) space solution 12 lines and easy to understand

  • 36

    The algorithm is a BFS or level order traversal. We go through the tree level by level. node is the pointer in the parent level, tail is the tail pointer in the child level.
    The parent level can be view as a singly linked list or queue, which we can traversal easily with a pointer.
    Connect the tail with every one of the possible nodes in child level, update it only if the connected node is not nil.
    Do this one level by one level. The whole thing is quite straightforward.


    def connect(self, node):
        tail = dummy = TreeLinkNode(0)
        while node:
   = node.left
                tail =
   = node.right
                tail =
            node =
            if not node:
                tail = dummy
                node =
    # 61 / 61 test cases passed.
    # Status: Accepted
    # Runtime: 100 ms
    # 95.26%

  • 0

    @dietpepsi Elegant! Great use of dummy pointer. Thanks for the solution!

  • 0

    Hi, thank you for sharing the code. I really have difficulty in understanding how the dummy works: it seems to me that dummy is a floating node all the time, and at the end of every level, node will become Null since it is assigned to Therefore, node will not pass the while condition for every level, actually it should get stuck for the 1st round (root node). I'm really struggling where I got wrong. Could you help to point out? Thank you so much!

  • 0

    For the benefit of others, here is more intuitive code with more clear names to clarify their meaning and purpose. Whole credit is to @dietpepsi whose this post actually inspired me to think of linked list as a queue

    class Solution:
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            if not root:
            sentinel = rear = TreeLinkNode(0)
            # enqueue root
            front = root
   = rear
            while front is not rear:
                # dequeue from front
                node  = front
                front =
                # if sentinel is dequeued
                if node is sentinel:
                    # if rear has non sentinel items
                    # enqueue sentinel back
                    if rear is not sentinel:
               = sentinel
                        rear =
                if is sentinel: # full level is traversed
           = None
                # enqueue left
                if node.left:
           = node.left
                    rear =
                # enqueue right
                if node.right:
           = node.right
                    rear =

  • 1

    @gaosuwoniu The first line where we make tail and dummy equal to same tree node that we newly created.

    tail = dummy = TreeLinkNode(0)

    Afterwards in while loop, first line we make equal to node.left which is the left node (of the current node being traversed) in original tree. So if python assignment works the same as pointers in C, in this line essentially we are also setting to node.left, because they are pointing towards same node. = node.left

    now in the last line of the function we are equating node = which is pointing to node.left since we did not move it at all in the entire loop. That is how its going to the next level.

  • 3
    My python solution
    def connect(self, root):
            while root:
                cur = tmp = TreeLinkNode(0)
                while root:
                    if root.left:
               = root.left
                        cur = root.left
                    if root.right:
               = root.right
                        cur = root.right
                    root =
                root =

  • 0
    This post is deleted!

Log in to reply

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