-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path99-recover-binary-search-tree.py
53 lines (47 loc) · 1.64 KB
/
99-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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
first_wrong = None
second_wrong = None
prev = None
cur = root
def check():
nonlocal first_wrong, second_wrong, prev, cur
if prev and prev.val > cur.val:
if not first_wrong:
first_wrong = prev
second_wrong = cur
prev = cur
while cur:
if not cur.left:
check()
cur = cur.right
else:
pred = cur.left
while pred.right and pred.right != cur:
pred = pred.right
if not pred.right:
pred.right = cur
cur = cur.left
else:
check()
pred.right = None
cur = cur.right
first_wrong.val, second_wrong.val = second_wrong.val, first_wrong.val
# time O(n), due to traverse
# space O(1)
# using tree and dfs (inorder and morris traverse)
'''
1. normal morris traversal
2. when have to add to list (not actually add), use prev_node to record
3. next time need to add to list (not actually add), take the prev_node to compare
4. there can have one pair error or two pairs error, so keep the first met error node and last met error node
'''