Python simple recursive preorder


  • 0
    A
    class Solution(object):
        def flatten(self, root):
           
            #global variable to remember previous node.
            self.prev = TreeNode(-1)
            self.preorder(root)
           
            #move left link-list to right link-list.
            while root:
                root.right = root.left
                root.left = None
                root = root.right
        
    
        def preorder(self, node):
            
            if not node: return None
    
            self.prev.left = node
            self.prev = node
            
            self.preorder(node.left)
            self.preorder(node.right)
            node.right = None
    

Log in to reply
 

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