**Binary Search Tree Typo - Microsoft Top Interview Questions**

### Problem Statement :

You are given a binary tree root which is almost a binary search tree except two nodes' values have been swapped. Return the original binary search tree. Constraints n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [2, [5, null, null], [7, [1, null, null], [8, null, null]]] Output [2, [1, null, null], [7, [5, null, null], [8, null, null]]] Explanation We can swap 1 and 5. Example 2 Input root = [0, [1, null, null], null] Output [1, [0, null, null], null] Explanation We can swap 0 and 1.

### Solution :

` ````
Solution in C++ :
void check(Tree* root, Tree*& prev, Tree*& node1, Tree*& node2) {
if (!root) return;
check(root->left, prev, node1, node2);
if (prev && prev->val >= root->val) {
if (node1 == nullptr) node1 = prev;
node2 = root;
}
prev = root;
check(root->right, prev, node1, node2);
}
Tree* solve(Tree* root) {
Tree *node1 = nullptr, *node2 = nullptr, *prev = nullptr;
check(root, prev, node1, node2);
swap(node1->val, node2->val);
return root;
}
```

` ````
Solution in Java :
import java.util.*;
/**
* public class Tree {
* int val;
* Tree left;
* Tree right;
* }
*/
class Solution {
public Tree solve(Tree root) {
List<Tree> values = new ArrayList<>();
inOrder(root, values);
findAndSwap(values);
return root;
}
private void inOrder(Tree node, List<Tree> values) {
if (node == null)
return;
inOrder(node.left, values);
values.add(node);
inOrder(node.right, values);
}
private void findAndSwap(List<Tree> values) {
Tree first = null;
Tree second = null;
for (int i = 1; i < values.size(); i++) {
if (values.get(i - 1).val > values.get(i).val) {
if (first == null) {
first = values.get(i - 1);
second = values.get(i);
} else {
second = values.get(i);
}
}
}
int temp = first.val;
first.val = second.val;
second.val = temp;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, root):
left = right = None
last_node = Tree(-float("inf"))
for node in self.morris_inorder(root):
if node.val < last_node.val:
left = left or last_node
right = node
last_node = node
left.val, right.val = right.val, left.val
return root
@staticmethod
def morris_inorder(node):
temp = None
while node:
if node.left:
temp = node.left
while temp.right and temp.right != node:
temp = temp.right
if temp.right:
temp.right = None
yield node
node = node.right
else:
temp.right = node
node = node.left
else:
yield node
node = node.right
```

## View More Similar Problems

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →## Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →