-
Notifications
You must be signed in to change notification settings - Fork 0
/
99.恢复二叉搜索树.py
46 lines (39 loc) · 1.26 KB
/
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
41
42
43
44
45
#
# @lc app=leetcode.cn id=99 lang=python3
#
# [99] 恢复二叉搜索树
#
# @lc code=start
# 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.
"""
self.x = None
self.y = None
self.pre = None
# 中序遍历二叉树,并比较上一个节点(pre)和当前节点的值,如果pre的值大于当前节点值,则记录下这两个节点
def dfs(root):
if not root:
return
dfs(root.left)
if not self.pre:
self.pre = root
else:
if self.pre.val>root.val:
self.y = root
if not self.x:
self.x = self.pre
self.pre = root
dfs(root.right)
dfs(root)
# 如果x和y都不为空,说明二叉搜索树出现错误的节点,将其交换
if self.x and self.y:
self.x.val,self.y.val = self.y.val,self.x.val
# @lc code=end