My simple accepted python solution with no extra space


  • 1
    V
    class Solution:
    def connect(self, root):
        if root is None:
            return
        if root.left != None:
            root.left.next = root.right
            if root.next == None:
                root.right.next = None
            else:
                root.right.next = root.next.left
            self.connect(root.left)
            self.connect(root.right)
        return
    

    Trying to make the full use of perfect binary tree.


  • 3
    S

    This uses recursion and hence there is stack consumption. Wonder if there is any alternative solution. Could be resolved using BFS but needs a queue which would consume space == number of elements in any given level. Any thoughts ?


Log in to reply
 

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