Given a binary search tree, the task is to flatten it to a sorted list in decreasing order. Precisely, the value of each node must be greater than the values of all the nodes at its right, and its left node must be NULL after flattening. We must do it in O(H) extra space where ‘H’ is the height of BST.

**Examples:**

Input:5 / \ 3 7 / \ / \ 2 4 6 8Output:8 7 6 5 4 3 2Input:1 \ 2 \ 3 \ 4 \ 5Output:5 4 3 2 1

**Approach:** A simple approach will be to recreate the BST from its ‘reverse in-order’ traversal. This will take O(N) extra space where N is the number of nodes in BST.

To improve upon that, we will simulate the reverse in-order traversal of a binary tree as follows:

- Create a dummy node.
- Create variable called ‘prev’ and make it point to dummy node.
- Perform reverse in-order traversal and at each step.
- Set prev -> right = curr
- Set prev -> left = NULL
- Set prev = curr

This will improve the space complexity to O(H) in the worst case as in-order traversal takes O(H) extra space.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Node of the binary tree ` `struct` `node { ` ` ` `int` `data; ` ` ` `node* left; ` ` ` `node* right; ` ` ` `node(` `int` `data) ` ` ` `{ ` ` ` `this` `->data = data; ` ` ` `left = NULL; ` ` ` `right = NULL; ` ` ` `} ` `}; ` ` ` `// Function to print flattened ` `// binary tree ` `void` `print(node* parent) ` `{ ` ` ` `node* curr = parent; ` ` ` `while` `(curr != NULL) ` ` ` `cout << curr->data << ` `" "` `, curr = curr->right; ` `} ` ` ` `// Function to perform reverse in-order traversal ` `void` `revInorder(node* curr, node*& prev) ` `{ ` ` ` `// Base case ` ` ` `if` `(curr == NULL) ` ` ` `return` `; ` ` ` `revInorder(curr->right, prev); ` ` ` `prev->left = NULL; ` ` ` `prev->right = curr; ` ` ` `prev = curr; ` ` ` `revInorder(curr->left, prev); ` `} ` ` ` `// Function to flatten binary tree using ` `// level order traversal ` `node* flatten(node* parent) ` `{ ` ` ` ` ` `// Dummy node ` ` ` `node* dummy = ` `new` `node(-1); ` ` ` ` ` `// Pointer to previous element ` ` ` `node* prev = dummy; ` ` ` ` ` `// Calling in-order traversal ` ` ` `revInorder(parent, prev); ` ` ` ` ` `prev->left = NULL; ` ` ` `prev->right = NULL; ` ` ` `node* ret = dummy->right; ` ` ` ` ` `// Delete dummy node ` ` ` `delete` `dummy; ` ` ` `return` `ret; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `node* root = ` `new` `node(5); ` ` ` `root->left = ` `new` `node(3); ` ` ` `root->right = ` `new` `node(7); ` ` ` `root->left->left = ` `new` `node(2); ` ` ` `root->left->right = ` `new` `node(4); ` ` ` `root->right->left = ` `new` `node(6); ` ` ` `root->right->right = ` `new` `node(8); ` ` ` ` ` `// Calling required function ` ` ` `print(flatten(root)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8 7 6 5 4 3 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Flatten BST to sorted list | Increasing order
- Flatten Binary Tree in order of Level Order Traversal
- Flatten binary tree in order of post-order traversal
- Find k-th smallest element in BST (Order Statistics in BST)
- Flatten Binary Tree in order of Zig Zag traversal
- Sorted order printing of a given array that represents a BST
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Flatten a binary tree into linked list
- Flatten a binary tree into linked list | Set-2
- Flatten a binary tree into linked list | Set-3
- Sorted Linked List to Balanced BST
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Two nodes of a BST are swapped, correct the BST
- Print array elements in alternatively increasing and decreasing order
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Sorted Array to Balanced BST
- Construct BST from its given level order traversal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.