Concise python solution, 9 lines, space O(1)


  • 9
    C

    level by level traversal with a dummy head prekid.

    root is in the current level, and kid is in the next level. prekid.next is the head in the kid level

    kid = kid.next or kid : Update kid ONLY when we actually find its next node



    runtime is around 96ms with a best runtime 88ms.

    def connect(self, root):
        prekid = kid = TreeLinkNode(0)
        while root:
            while root:
                kid.next = root.left
                kid = kid.next or kid
                kid.next = root.right
                kid = kid.next or kid
                root = root.next
            root, kid = prekid.next, prekid

  • 0
    Y

    @cmc The best python solution I see for this question.


Log in to reply
 

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