Iteration Python solution


  • 4
    S
     class Solution:
            # @param root, a tree node
            # @return root of the upside down tree
            def upsideDownBinaryTree(self, root):
                # take care of the empty case
                if not root:
                    return root
                # take care of the root
                l = root.left
                r = root.right
                root.left = None
                root.right = None
                # update the left and the right children, form the new tree, update root
                while l:
                    newL = l.left
                    newR = l.right
                    l.left = r
                    l.right = root
                    root = l
                    l = newL
                    r = newR
                return root

Log in to reply
 

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