-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path99.恢复二叉树.py
40 lines (38 loc) · 997 Bytes
/
99.恢复二叉树.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
# 二叉搜索树中的两个节点被错误地交换。
#
# 请在不改变其结构的情况下,恢复这棵树。
#
# 示例 1:
#
# 输入: [1, 3, null, null, 2]
#
# 1
# /
# 3
# \
# 2
#
# 输出: [3, 1, null, null, 2]
#
# 3
# /
# 1
# \
# 2
class Solution:
def recoverTree(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
cur,prev,drops = root,TreeNode(float('-inf')),[]
while cur:
if cur.left:
temp = cur.left
while temp.right and temp.right != cur:temp = temp.right
if not temp.right:
temp.right,cur = cur,cur.left
continue
temp.right = None
if cur.val < prev.val:drops.append((prev,cur))
prev,cur = cur,cur.right
drops[0][0].val,drops[-1][1].val = drops[-1][1].val,drops[0][0].val