-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover-binary-search-tree.py
93 lines (84 loc) · 2.62 KB
/
recover-binary-search-tree.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Solution(object):
def recoverTree(self, root):
"""
:type root: TreeNode
:rtype: None Do not return anything, modify root in-place instead.
"""
res = []
val = []
def dfs(root):
if not root:
return
dfs(root.left)
res.append(root)
val.append(root.val)
dfs(root.right)
dfs(root)
index1 = -1
index2 = -1
for i in xrange(0,len(val)-1):
if val[i]>val[i+1]:
if index1==-1:
index1 = i
index2 = i+1
else:
index2 = i+1
if index1>-1 and index2>-1:
tmp = res[index1].val
res[index1].val = res[index2].val
res[index2].val = tmp
# O(h)空间复杂度的解法
def recoverTree(self, root):
self.index1 = None
self.index2 = None
self.pre = None
def dfs(root):
if not root:
return
dfs(root.left)
if not self.pre:
self.pre = root
if self.pre.val<=root.val:
self.pre = root
else:
if not self.index1:
self.index1 = self.pre
self.index2 = root
else:
self.index2 = root
dfs(root.right)
dfs(root)
tmp = self.index1.val
self.index1.val = self.index2.val
self.index2.val = tmp
# 莫里斯遍历,遍历后恢复二叉树,真正的O(1)空间复杂度
def recoverTree(self, root):
x = None
y = None
morris_tmp = None
pre = None
while root:
if root.left:
morris_tmp = root.left
while morris_tmp.right and morris_tmp.right!=root:
morris_tmp = morris_tmp.right
if morris_tmp.right is None:
morris_tmp.right = root
root = root.left
else:
if pre and pre.val>root.val:
y = root
if not x:
x = pre
pre = root
morris_tmp.right = None
root = root.right
else:
if pre and pre.val>root.val:
y = root
if not x:
x = pre
pre = root
root = root.right
if x and y:
x.val,y.val = y.val,x.val