AfterAcademy Tech
•
24 Dec 2019

Asked in: Amazon, Microsoft, Adobe
Difficulty: Medium
Problem Description: Given a binary tree, flatten it to a linked list in-place. After flattening, the left of each node should point to NULL and right should contain the next node in pre-order so that it resembles a singly linked list.
Example 1

Example 2

By looking at the example shown, it is very obvious that the head of the output linked list is the root of the tree node, followed by a flattened left subtree, which is followed by a flattened right subtree.
The node structure passed to your function will be
class TreeNode
{
int data
TreeNode left
TreeNode right
}
Possible follow up questions to ask the interviewer —
Hint → If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal.
We will see three different approaches to solve this problem →
If you observe how the tree gets flatten then it is clear that for each subtree’s root node
Solution steps —

Pseudo Code
void flatten(TreeNode root)
{
// reference temporary variable
TreeNode temp = {0, NULL, NULL}
reverse_preorder(root, temp);
delete temp
}
void reverse_preorder(TreeNode root, TreeNode &temp)
{
if(root is NULL)
return
// process right sub-tree
reverse_preorder(root.right, temp)
// process left sub-tree
reverse_preorder(root.left, temp)
// set the right child of root with the left flattened tree
root.right = temp
// set the left child with null
root.left = NULL
// set the temp variable with the current node
temp = root
}
Complexity analysis
Time Complexity: O(n) Each node is visited exactly once and the operation performed on each node is of constant time.
Space Complexity: O(h), where h is the height of the tree
Critical ideas to think
The recursive solution could be solved iteratively using stack where at any instant the top of the stack will represent the root of a subtree.
Solution steps —
Pseudo Code
void flatten(TreeNode root)
{
if(root is NULL)
return
stack S
S.push(root)
while(S is not empty)
{
TreeNode subroot = S.pop()
if(subroot.right)
S.push(subroot.right)
if(subroot.left)
S.push(subroot.left)
if(S is not empty)
subroot.right = S.peek()
subroot.left = null
}
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Critical ideas to think
Each time when we prune a right subtree, we use a while-loop to find the right-most leaf of the current left subtree and append the subtree there.
Pseudo Code
void flatten(TreeNode root)
{
TreeNode cur = root
while (cur != null)
{
if (cur.left != null)
{
// if we need to prune a right subtree
if (cur.right != null)
{
TreeNode next = cur.left
while (next.right != null)
next = next.right
next.right = cur.right
}
cur.right = cur.left
cur.left = null
}
cur = cur.right
}
}
Complexity Analysis
Visit each node at most twice (one for flattening and one for looking for rightmost leaf) and then for each node, cut the right tree and append it to its rightmost node. Overall, access to each node is in constant time.
Time Complexity: O(n)
Space Complexity: O(1)
Critical ideas to think

Any feedback or suggestions are welcome in the comment section.
Happy Coding!! Enjoy Algorithms.
AfterAcademy Tech
In this blog, we will discuss the types of linked list and basic operations that can be performed on a linked list.

AfterAcademy Tech
The problem is about reversing a Linked List. We are given the head node of the Linked List and we have to return the reversed Linked List's head. This is an interview problem asked in companies like Microsoft, Amazon and Adobe.

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 a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
