Depend on level order use only 8 lines


  • 0
    W

    class Solution:
    # @param root, a tree link node
    # @return nothing
    def connect(self, root):
    if not root:
    return
    root.next = None
    level = [root]
    while level:
    for i in range(len(level)-1):
    level[i].next = level[i+1]
    level = [kid for l in level for kid in (l.left, l.right) if kid]


Log in to reply
 

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