Last Updated: 12 Feb, 2021

Flatten BST To A Sorted List

Moderate
Asked in companies
AdobeAmazonRakuten India

Problem statement

You have been given a Binary Search Tree (BST). Your task is to flatten the given BST to a sorted list. More formally, you have to make a right-skewed BST from the given BST, i.e., the left child of all the nodes must be NULL, and the value at the right child must be greater than the current node.

A binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree whose internal nodes each store a value greater than all the values in the node's left subtree and less than those in its right subtree.

Follow Up :
Can you solve this in O(N) time and O(H)  space complexity?
Input Format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run. 

Then the T test cases follow. 

The first line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :

bst_example

4
2 6
1 3 5 7
-1 -1 -1 -1 -1 -1 -1 -1
Explanation :
Level 1 :
The root node of the tree is 4

Level 2 :
Left child of 4 = 2
Right child of 4 = 6

Level 3 :
Left child of 2 = 1
Right child of 2 = 3
Left child of 6 = 5
Right child of 6 = 7

Level 4 :
Left child of 1 = null (-1)
Right child of 1 = null (-1)
Left child of 3 = null (-1)
Right child of 3 = null (-1)
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
Output Format :
For each test case, flatten the BST and print the values of the nodes in the level order form.

Print the output of each test case in a separate line.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100
1 <= N <= 5000
0 <= node.data <= 10^9, (where node data != -1)

Where 'N' denotes the number of nodes in the given tree.

Time Limit: 1 second

Approaches

01 Approach

Note that the inorder traversal of a BST is always sorted. So, we can traverse the BST in an inorder manner and store the values in an array. Then we create a new node for each element in the array, make its left child NULL and right child equal to the node containing the next element in the array. For the last element, we make both the left and right child NULL.

02 Approach

We can easily optimize the above approach in terms of space complexity by maintaining a previous pointer. While traversing the BST in an inorder manner, we can make the left child of the previous as NULL and the right child of the previous as the current node. Then update the previous to the current node.

 

Algorithm

 

  • If root == NULL, then return root.
  • Create a temp pointer and, which will act as the initial previous pointer, and store a dummy value in it. As the nodes can’t have a value -1 in this problem, we can keep the value -1 in this temp pointer.
  • Create the previous pointer and make it point to temp initially.
  • Call the inorder function passing both the root and previous as arguments, i.e., inorder(root, previous).
  • Then at this point, the previous is pointing to the last node of the tree, so we make the left and the right child of the previous NULL.
  • Then, we create a pointer newRoot, which is the root of the resulting BST. Make newRoot = temp.right. As temp contains a dummy value, which doesn’t belong to the actual BST.
  • Then delete the temp pointer to avoid memory leak and return newRoot.

 

inorder(root, previous):

 

  • If root == NULL, then return.
  • Call the function recursively for the left child, i.e., inorder(root.left, previous).
  • Make the left child of previous NULL, i.e., previous.left = NULL.
  • Make the right child of previous as root and make the previous point to root, i.e., previous.right = root and previous = root.
  • Then, call the function recursively for the right child, i.e., inorder(root.right, previous).