An 10-line easily understandable Python solution


  • 0
    C

    1: link every left child to right child
    2: link every right child to the left child of his father's next

        def connect(self, root):
            if root==None:
                return
            def connect1(r):
                if r.left:   r.left.next=r.right
                if r.right and r.next:   r.right.next=r.next.left
                if r.left and r.right:
                    connect1(r.left)
                    connect1(r.right)
            connect1(root)
    

Log in to reply
 

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