码迷,mamicode.com
首页 > 其他好文 > 详细

099 Recover Binary Search Tree

时间:2015-08-06 07:04:10      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

099 Recover Binary Search Tree

没有按照要求用 constant space.... 用valid BST 找出两个not in order 的nodes

class Solution:
    def recoverTree(self, root):
        [fN, sN] = self.valid(root)
        fN.val, sN.val = sN.val, fN.val

    def valid(self, root):
        pre, fN, sN = TreeNode(-100000), None, None
        stack = []
        while True:
            while root != None:
                stack.append(root)
                root = root.left
            if stack == []:
                break
            root = stack.pop()
            if pre.val >= root.val:
                if not fN:
                    fN = pre
                    sN = root
                else:
                    sN = root
                    break
            pre = root
            root = root.right
        return [fN, sN]

 

099 Recover Binary Search Tree

标签:

原文地址:http://www.cnblogs.com/dapanshe/p/4706315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!