forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution of Recovering Binary Search Tree
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
''' | ||
Two elements of a binary search tree (BST) are swapped by mistake. | ||
Recover the tree without changing its structure. | ||
Example 1: | ||
Input: [1,3,null,null,2] | ||
1 | ||
/ | ||
3 | ||
\ | ||
2 | ||
Output: [3,1,null,null,2] | ||
3 | ||
/ | ||
1 | ||
\ | ||
2 | ||
''' | ||
# Definition for a binary tree node. | ||
# class TreeNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution(object): | ||
def recoverTree(self, root): | ||
""" | ||
:type root: TreeNode | ||
:rtype: void Do not return anything, modify root in-place instead. | ||
""" | ||
|
||
first, second, prev = None, None, None | ||
def inorder(root): | ||
if root: | ||
inorder(root.left) | ||
if prev is not None and root.val < prev.val: | ||
if first is None: | ||
first = root | ||
else: | ||
second = root | ||
prev = root | ||
inorder(root.right) | ||
|
||
|
||
inorder(root) | ||
if first and second: | ||
first.val, second.val = second.val, first.val |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters