AfterAcademy Tech
•
16 Jun 2020

Difficulty: Hard
Asked in: Amazon, Microsoft
Given a Binary Search Tree such that two of the nodes of this tree have been swapped by mistake. You need to write a program that will recover this BST while also maintaining its original structure.
For Example:
Input: Given a BST in which two of the nodes are swapped by mistake.
10
/ \
7 9
/ \
5 15
Output:
10
/ \
7 15
/ \
5 9
Explanation: As you can easily see, in the above binary search tree, the two nodes 9 and 15 have been swapped with each other. This has been fixed in the output tree.
Possible questions to ask the interviewer:
Before proceeding to solutions below, don’t you want to give this problem a try? Click here.
We are going to discuss two possible solutions for this problem. We will start with a simple naive solution that will definitely come to our minds and then will see its complexity and will come up with a less complex, efficient solution.
Solution idea
The idea is fairly simple, we know that in-order traversal of the binary search tree will give us elements in the sorted order. Now, in this case, since the two elements are swapped with each other, the in-order traversal will not result in a list of sorted elements. Better say, there will be two misplacements of elements in the sorted list. By taking the in-order traversal of the given BST and sorting it, we can easily have a check on the swapped elements. This check will now help us in finding the corresponding nodes in the tree and exchanging it with each other. This will help us in recovering the original tree and we will also be storing the structure of the given tree.
Solution steps
Pseudo-code
void inorderTraversal(TreeNode root, int arrInorder[])
{
inorderTraversal(root.left)
arrInorder.add(root.data)
inorderTraversal(root.right)
}
void searchAndUpdate(TreeNode root, int element1, int element2)
{
if(root == NULL)
return
searchAndUpdate(root.left, element1, element2)
if(root.data == element1)
root.data = element2
else if(root.data == element2)
root.data == element1
searchAndUpdate(root.right, element1, element2)
}
void fixBst(TreeNode root)
{
int[] arrInorder
inorderTraversal(root, arrInorder)
int[] copyArr = arrInorder.copy() //copies array into new one
copyArr.sort() //will sort the array
for(int i = 0 to arrInorder.length; i+=1)
{
if(arrInorder[i] != copyArr)
searchAndUpdate(root, arrInorder[i], copyArr[i])
break
}
}
Complexity Analysis
Time Complexity: O(logN) (We are sorting the list here but do you think the complexity will go upto logN? Hint: The array will be almost sorted.)
Space Complexity: O(N)
Critical ideas to think!
Solution idea
In the above solution, we were comparing the entire sorted array which we got from the in-order traversal of the BST. Since we know that the in-order traversal of a BST will always give us a sorted list of elements, this problem can be reduced to a problem where two elements of a sorted array have been swapped.
For example:
In-order Traversal of the given BST {5, 7, 9, 10, 15}
In-order Traversal of the BST when 9 and 15 are swapped {5, 7, 15, 10, 9}
Looking at the above example, you can conclude that maintaining two-pointers and updating the pointers whenever you encounter distortion in the sorted array will give you two swapped elements. This seems okay till now. But look at the below given in-order traversal of a BST and the in-order traversal after the two nodes are swapped.
In-order Traversal of the BST {5, 7, 9, 12, 16}
In-order Traversal of the BST after two nodes 7 and 9 are swapped
{5, 9, 7, 12, 16}
Do you still think that our previous approach will work in this case? No, right? So what should be our ideal approach then? We will maintain three-pointers first, middle, and last. Every time when we will update our first pointer (when the previous node’s data is greater than than the current node), we will update the middle one also with the current node. If we encounter a similar situation anywhere else and our first pointer is already being updated we will update the last pointer with the current node. In case of adjacent elements, our last pointer will be NULL. And our first and middle pointers will be having the values of swapped elements.
Solution steps
Pseudo-code
void fixBstUtil(TreeNode root, TreeNode first, TreeNode middle, TreeNode last, TreeNode previous)
{
if(root is NULL) return
fixBstUtil(root.left, first, middle, last, previous)
if(previous and previous.data > root.data)
{
if(first is NULL)
{
first = previous
middle = root
}
else
last = root
previous = root
}
fixBstUtil(root.right, first, middle, last, previous)
}
void fixBst(TreeNode root)
{
TreeNode first, middle, last, previous
first = middle = last = previous = NULL
fixBstUtil(root, first, middle, last, previous)
if(first and last)
swap(first.data, last.data) //function for swapping values
else
swap(first.data, middle.data)
}
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1)
Critical ideas to think!

Happy Coding!
Team AfterAcademy!!
AfterAcademy Tech
Given a binary tree, invert the binary tree and return it. An inverted form of a Binary Tree is another Binary Tree with left and right children of all non-leaf nodes interchanged. You may also call it the mirror of the input tree.

AfterAcademy Tech
Given the root of a binary tree, check whether it is a binary search tree or not.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft. The problem is to merge two binary trees. The overlapping nodes are replaced by the sum of corresponding nodes. We are discussing two ways of solving this.

AfterAcademy Tech
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
