Simple Readable Python Solution


  • 0
    M
        def flatten(self, root):
            """
            :type root: TreeNode
            :rtype: void Do not return anything, modify root in-place instead.
            """
            nodes = []
            
            def getNodes(node):
                if node:
                    nodes.append(node)
                    if node.left:
                        getNodes(node.left)
                    if node.right:
                        getNodes(node.right)
            
            getNodes(root)
            
            for i in xrange(len(nodes) - 1):
                node = nodes[i]
                node.right = nodes[i+1]
                node.left = None
    

Log in to reply
 

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