-
Notifications
You must be signed in to change notification settings - Fork 1
/
12. RectifyBST.py
31 lines (25 loc) · 955 Bytes
/
12. RectifyBST.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
from typing import Optional
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:
self.first = None
self.second = None
self.prev = TreeNode(-int(10e9))
def inorder(root):
if root:
inorder(root.left)
# Logic
if self.first == None and self.prev.val >= root.val:
self.first = self.prev
if self.first != None and self.prev.val >= root.val:
self.second = root
self.prev = root
# End of Logic
inorder(root.right)
inorder(root)
self.first.val, self.second.val = self.second.val, self.first.val # type: ignore
# Link: https://leetcode.com/problems/recover-binary-search-tree/