Question for Python


  • 0
    L

    I am seeing a weird thing... can anybody explain??

    why this doesn't work

    def upsideDownBinaryTree(self, root):
        if not root:
            return None
        if not root.left:
            return root
    
        node = self.upsideDownBinaryTree(root.left)
        root_new = self.upsideDownBinaryTree(root.left)
        
        while node.right:
            node = node.right
        
        node.left = root.right
        node.right = root
        node.right.left = None
        node.right.right = None
        
        return root_new
    

    but when i move root_new in front of node, it passes

    def upsideDownBinaryTree(self, root):
        if not root:
            return None
        if not root.left:
            return root
       
        root_new = self.upsideDownBinaryTree(root.left)
        node = self.upsideDownBinaryTree(root.left)
        
        while node.right:
            node = node.right
        
        node.left = root.right
        node.right = root
        node.right.left = None
        node.right.right = None
        
        return root_new

Log in to reply
 

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