10-line Python iterative solution


  • 0
    C
    def flatten(self, root):
        nstack = []
        while root:
            if root.left:
                if root.right:
                    nstack.append(root.right)
                root.right, root.left = root.left, None
            if not root.right and nstack:
                root.right = nstack.pop()
            root = root.right

Log in to reply
 

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