Python easy 6-lines iterative

  • 0
    def flatten(self, root):
            while root:
                if root.left:
                    right, root.right, tmp = root.right, root.left, root
                    while tmp.right: tmp = tmp.right
                    tmp.right, root.left = right, None
                root = root.right

    idea is simple:
    switch the left subtree to the right side and append the original right subtree under the right most leaf of the original left subtree

Log in to reply

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