my clear and easy understand python solution


  • 0
    G
    class Solution:
        # @param root, a tree link node
        # @return nothing
        def connect(self, root):
            left = root
            cur = None
            while left:
                cur = left
                while cur:
                    if cur.left:
                        cur.left.next = cur.right or self.find_next_node(cur.next)
                    if cur.right:
                        cur.right.next = self.find_next_node(cur.next)
                    cur = cur.next
                left = self.find_next_node(left)
            
        def find_next_node(self, node):
            while node:
                if node.left:
                    return node.left
                if node.right:
                    return node.right
                node = node.next
            return None
    

Log in to reply
 

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