# Flatten Binary Tree in order of Level Order Traversal

Given a Binary Tree, the task is to flatten it in order of Level order traversal of the tree. In the flattened binary tree, the left node of all the nodes must be NULL.

**Examples:**

Input:1 / \ 5 2 / \ / \ 6 4 9 3Output:1 5 2 6 4 9 3Input:1 \ 2 \ 3 \ 4 \ 5Output:1 2 3 4 5

**Approach:** We will solve this problem by simulating the Level order traversal of Binary Tree as follows:

- Create a queue to store the nodes of Binary tree.
- Create a variable “prev” and initialise it by parent node.
- Push left and right children of parent in the queue.
- Apply level order traversal. Lets say “curr” is front most element in queue. Then,
- If ‘curr’ is NULL, continue.
- Else push curr->left and curr->right in the queue
- Set prev = curr

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 flatten Binary tree using ` `// level order traversal ` `void` `flatten(node* parent) ` `{ ` ` ` `// Queue to store nodes ` ` ` `// for BFS ` ` ` `queue<node*> q; ` ` ` `q.push(parent->left); ` ` ` `q.push(parent->right); ` ` ` `node* prev = parent; ` ` ` ` ` `// Code for BFS ` ` ` `while` `(q.size()) { ` ` ` ` ` `// Size of queue ` ` ` `int` `s = q.size(); ` ` ` `while` `(s--) { ` ` ` ` ` `// Front most node in ` ` ` `// the queue ` ` ` `node* curr = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// Base case ` ` ` `if` `(curr == NULL) ` ` ` `continue` `; ` ` ` `prev->right = curr; ` ` ` `prev->left = NULL; ` ` ` `prev = curr; ` ` ` ` ` `// Pushing new elements ` ` ` `// in queue ` ` ` `q.push(curr->left); ` ` ` `q.push(curr->right); ` ` ` `} ` ` ` `} ` ` ` ` ` `prev->left = NULL; ` ` ` `prev->right = NULL; ` `} ` ` ` `// Function to print flattened ` `// Binary Tree ` `void` `print(node* parent) ` `{ ` ` ` `node* curr = parent; ` ` ` `while` `(curr != NULL) ` ` ` `cout << curr->data << ` `" "` `, curr = curr->right; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `node* root = ` `new` `node(1); ` ` ` `root->left = ` `new` `node(5); ` ` ` `root->right = ` `new` `node(2); ` ` ` `root->left->left = ` `new` `node(6); ` ` ` `root->left->right = ` `new` `node(4); ` ` ` `root->right->left = ` `new` `node(9); ` ` ` `root->right->right = ` `new` `node(3); ` ` ` ` ` `// Calling required functions ` ` ` `flatten(root); ` ` ` `print(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Time Complexity:** O(N)

**Space Complexity:** O(N) where N is the size of Binary Tree.

## Recommended Posts:

- Flatten binary tree in order of post-order traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Zig Zag traversal
- Density of Binary Tree using Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Perfect Binary Tree Specific Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Level Order Tree Traversal
- Zig Zag Level order traversal of a tree using single queue
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Level Order Successor of a node in Binary Tree
- Level Order Predecessor of a node in Binary Tree
- Print even positioned nodes of even levels in level order of the given binary tree

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.