python solution

  • 0
    class Solution:
        def connect(self, root):
            if root is None: return None
            q = [root]
            while q:
                previous = None
                for _ in range(len(q)):
                    node = q.pop()
                    if previous:
               = node
                        previous = node
                        previous = node
                    if node.left:
                        q.insert(0, node.left)
                    if node.right:
                        q.insert(0, node.right)

Log in to reply

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