# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a tree node def recoverTree(self, root): if not root: return None pre=parent=f1=f2=None# dummy node foundfirst=False crt=root while crt: if crt.left: pre=crt.left while pre.right and pre.right!=crt: pre=pre.right if not pre.right: pre.right=crt crt=crt.left else: pre.right=None if parent.val>crt.val: if not foundfirst: f1=parent foundfirst=True f2=crt parent=crt crt=crt.right else: if parent and parent.val>crt.val: if not foundfirst: f1=parent foundfirst=True f2=crt parent=crt crt=crt.right if f1 and f2: f1.val,f2.val=f2.val,f1.val return root
Friday, September 12, 2014
Leetcode: Recover Binary Search Tree @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment