AfterAcademy Tech
•
10 Dec 2019

Asked in: Google, Amazon
Difficulty: Medium
Problem description: Invert a binary tree.
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.
NOTE: The tree is binary so there could be a maximum of two child nodes. This problem was originally inspired by this tweet by Max Howell .
Now, figure out that what does the interviewer mean when he says invert a binary tree, it would be helpful when you start looking at an example.
Example 1

Example 2

Explanation : You can observe that the root’s left pointer started pointing towards the right child and the right pointer towards the left child and a similar condition is noticed for all the sub root nodes.
The node structure for the BST passed to your function will be
class TreeNode
{
int val
TreeNode left
TreeNode right
}
We will be discussing three approaches to solve this problem
The key insight here is to realize that in order to invert a binary tree we only need to swap the children and recursively solve the two smaller sub-problems (same problem but for smaller input size) of left and right sub-tree. This looks similar to the idea of pre-order traversal.
The steps to be followed are :
Pseudo Code
// Function to invert given binary Tree using preorder traversal
void invertBinaryTree(TreeNode root)
{
// base case: if tree is empty
if (root == Null)
return
// swap left subtree with right subtree
swap(root.left, root.right)
// invert left subtree
invertBinaryTree(root.left)
// invert right subtree
invertBinaryTree(root.right)
}
Complexity Analysis
In the above approach, we are traversing each node of the tree only once. Time complexity: O(n)
Space Complexity of this algorithm is proportional to the maximum depth of recursion tree generated which is equal to the height of the tree (h).
Space complexity: O(h) for recursion call stack, where h is the height of the tree.
Critical ideas to think
Here, a Iterative Preorder Traversal is used to traverse the tree using a LIFO stack. To convert recursive procedures to iterative, we need an explicit stack during the implementation.
Solution Steps
Right child is pushed before left child to make sure that left subtree is processed first.
Pseudo Code
// Iterative Function to invert given binary Tree using stack
void invertBinaryTree(TreeNode root)
{
// base case: if tree is empty
if (root is null)
return
// create an empty stack and push root node
stack S
S.push(root)
// iterate until the stack is not empty
while (S is not empty)
{
// pop top node from stack
TreeNode curr = S.top()
S.pop()
// swap left child with right child
swap(curr.left, curr.right)
// push right child of popped node to the stack
if (curr.right)
S.push(curr.right)
// push left child of popped node to the stack
if (curr.left)
S.push(curr.left)
}
}
Complexity Analysis
For each node in the tree, we are performing push() and pop() operation only once. Total no of stack operations = 2n (Think!)
Time complexity: O(n), where n is the total number of nodes.
Space Complexity: O(n)(Think!)
Critical Ideas to think
We can solve this problem using Level Order Traversal. Here we the traverse the tree using a FIFO Queue.
In each iteration :
if (curr.left)
Q.enqueue(curr.left)
if (curr.right)
Q.enqueue(curr.right)
Pseudo Code
// Iterative Function to invert given binary Tree using queue
void invertBinaryTree(TreeNode root)
{
// base case: if tree is empty
if (root is NULL)
return
// maintain a queue and push push root node
queue Q
Q.enqueue(root)
// iterate untill queue is not empty
while (Q is not empty)
{
// pop top node from queue
TreeNode curr = Q.dequeue()
// swap left child with right child
swap(curr.left, curr.right)
// push left child of popped node to the queue
if (curr.left)
Q.enqueue(curr.left)
// push right child of popped node to the queue
if (curr.right)
Q.enqueue(curr.right)
}
}
Complexity Analysis
For each node in the tree, we are performing enqueue() and dequeue() operation only once. Total no of queue operations = 2n (Think!)
Time complexity: O(n), where n is the total number of nodes.
Space Complexity: O(n) (Think!)
Critical Ideas to think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
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 the root of a binary tree, check whether it is a binary search tree or not.

AfterAcademy Tech
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

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.
