class Solution: # @param root, a tree link node # @return nothing def connect(self, root): if not root or not root.left: return root.left.next = root.right if root.next: root.right.next = root.next.left self.connect(root.left) self.connect(root.right)
Even just the managing of recursion costs memory proportional to the recursion depth. And you do explicitly add to the stack with the
root argument. Every recursive call has its own
root reference. And they have to be remembered somewhere.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.