python solution


  • 0
    A
    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:
                        previous.next = node
                        previous = node
                    else:
                        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.