Skip to content

Commit 2c9d0b6

Browse files
authored
Merge pull request #949 from Anshika14528/999
Added Leetcode Problem :99 #879
2 parents b1b7b48 + bdb7829 commit 2c9d0b6

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
id: recover-binary-search-tree
3+
title: Recover Binary Search Tree
4+
sidebar_label: 0099 Recover Binary Search Tree
5+
tags:
6+
- tree
7+
- recursion
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Recover Binary Search Tree problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
You are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
23+
Input: root = [1,3,null,null,2]
24+
Output: [3,1,null,null,2]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: root = [3,1,4,null,null,2]
31+
Output: [2,1,4,null,null,3]
32+
```
33+
34+
35+
36+
### Constraints
37+
38+
- The number of nodes in the tree is in the range $[2, 1000]$.
39+
- $-2^(31) \leq \text{Node.val} \leq 2^(31) - 1$.
40+
- Solve in O(1)space.
41+
### Approach
42+
43+
To solve this problem(recover BST) as we know that the inorder traversal of binary search tree give us sorted array so when we traverse the array from left to right we will get to know that at some node the previous value will be greater than current value and that will be our first point were order is disturbed store current and its previous node so that we can counter if its an adjacent swap and if the order is disturbed for the second time, just store that node.After all this just swap the first and last value as you can in the below given code.
44+
45+
#### Code in C++
46+
47+
```cpp
48+
/**
49+
* Definition for a binary tree node.
50+
* struct TreeNode {
51+
* int val;
52+
* TreeNode *left;
53+
* TreeNode *right;
54+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
55+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
56+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
57+
* right(right) {}
58+
* };
59+
*/
60+
class Solution {
61+
public:
62+
void recover(TreeNode* root, TreeNode*& prev, TreeNode*& first,
63+
TreeNode*& last) {
64+
if (root == NULL) {
65+
return;
66+
}
67+
recover(root->left, prev, first, last);
68+
if (prev != NULL) {
69+
if (prev->val > root->val) {
70+
if (first == NULL) { // storing where the order is disturbed
71+
first = prev;
72+
}
73+
74+
last = root;
75+
}
76+
}
77+
prev = root;
78+
recover(root->right, prev, first, last);
79+
}
80+
void recoverTree(TreeNode* root) {
81+
TreeNode *prev = NULL, *first = NULL, *last = NULL;
82+
recover(root, prev, first, last);
83+
swap(first->val, last->val); //swapping after we get final values where order is distrubed
84+
return;
85+
}
86+
};
87+
```
88+
89+

0 commit comments

Comments
 (0)