Python iterative solution with the dummy root


  • 1
    L
    def upsideDownBinaryTree(self, root):
            stack = []
            while root:
                stack.append(root)
                root = root.left
            dummy = TreeNode(None)
            curr = dummy
            while stack:
                node = stack.pop()
                curr.right = node
                curr.left = node.right
                curr = curr.right
            curr.left = curr.right = None
            return dummy.right

Log in to reply
 

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