In place python solution, not recursive. revision code from a java solution


  • 1
    I
    class Solution:
        # @param root, a tree node
        # @return nothing, do it in place
        def flatten(self, root):
            head = TreeNode(0)
            head.right = root
            node = head
            while(node.right!=None):
                node = node.right
                if(node.left!=None):
                    end = node.left
                    while(end.right!=None):
                        end = end.right
                    temp = node.right
                    node.right = node.left
                    node.left = None
                    end.right = temp
            head.right = None

Log in to reply
 

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