Simple Python solution


  • 0
    A
    # class TreeLinkNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    #         self.next = None
    
    class Solution:
        def get_children(self,lst):
            succ = []
            for ele in lst:
                # No need to check both as its always a balanced tree
                if ele.left is not None:
                    succ.append(ele.left)
                    succ.append(ele.right)
            return succ
        
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            if root is not None:
                succ = [root]
                while len(succ) > 0:
                    succ = self.get_children(succ)
                    for i in range(1,len(succ)):
                        succ[i-1].next = succ[i]  
    

Log in to reply
 

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