Python simple solution


  • 0
    A
    class Solution:
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            
            if not root: return root
            
            layer = [root]
            
            while layer:
    
                newlayer = []
                n0 = None
                
                for node in layer:
                    
                    node.next = n0
                    n0 = node
                    
                    if node.right:
                        newlayer.append(node.right)
                    if node.left:
                        newlayer.append(node.left)
                        
                layer = newlayer
            
        
        
    

Log in to reply
 

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