GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/099_Recover_Binary_Search_Tree.py at master · pengaoao/leetcode Expand file tree/ Copy path099_Recover_Binary_Search_Tree.pyMore file actions 67 lines (58 loc) · 2.27 KB / Copy path099_Recover_Binary_Search_Tree.pyFile metadata and controls67 lines (58 loc) · 2.27 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): # def recoverTree(self, root): # """ # :type root: TreeNode # :rtype: void Do not return anything, modify root in-place instead. # """ # # https://discuss.leetcode.com/topic/9305/detail-explain-about-how-morris-traversal-finds-two-incorrect-pointer/2 # pre = first = second = None # while root is not None: # if root.left is not None: # temp = root.left # while temp.right is not None and temp.right != root: # temp = temp.right # if temp.right is not None: # if pre is not None and pre.val > root.val: # if first is None: # first = pre # second = root # pre = root # temp.right = None # root = root.right # else: # temp.right = root # root = root.left # else: # if pre is not None and pre.val > root.val: # if first is None: # first = pre # second = root # pre = root # root = root.right # # only two elements are swapped # if first is not None and second is not None: # first.val, second.val = second.val, first.val # https://discuss.leetcode.com/topic/3988/no-fancy-algorithm-just-simple-and-powerful-in-order-traversal/2 def __init__(self): self.first = self.second = None self.pre = TreeNode(-sys.maxint - 1) def recoverTree(self, root): self.traverse(root) self.first.val, self.second.val = self.second.val, self.first.val def traverse(self, root): if root is None: return self.traverse(root.left) if self.pre.val >= root.val: if self.first is None: self.first = self.pre if self.first is not None: self.second = root self.pre = root self.traverse(root.right) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |