My python solution, nothing special, just post~


  • 0
    import sys
    class Solution(object):
        def in_order(self,last_node, root):
            left_last_node = self.in_order(last_node, root.left) if root.left else last_node
            if left_last_node.val>root.val:
                self.reverse_list.append(left_last_node)
                self.reverse_list.append(root)
            return self.in_order(root, root.right)if root.right else root
    
        def recoverTree(self, root):
            self.reverse_list = []
            virtual_node = TreeNode(-sys.maxint)
            self.in_order(virtual_node, root)
            n1, n2 = (self.reverse_list[0], self.reverse_list[1]) if len(self.reverse_list)==2 else (self.reverse_list[0],self.reverse_list[-1])
            n1.val, n2.val = n2.val, n1.val
    
    

  • 0
    S

    Intricate design but takes long time to understand


Log in to reply
 

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