python leetcode 99. Recover Binary Search Tree

中序遍历,然后把每个值依照排序后的数值替换即可

class Solution(object):
    def recoverTree(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
        mylist=[]
        myTree=[]
        self.inorder(root,mylist,myTree)
        mylist.sort()
        for i in range(len(mylist)):
            myTree[i].val=mylist[i]
    def inorder(self,root,mylist,myTree):
        if root:
            self.inorder(root.left,mylist,myTree)
            mylist.append(root.val) 
            myTree.append(root)
            self.inorder(root.right,mylist,myTree)

你可能感兴趣的:(leetcode,python)