# Print Binary Tree levels in sorted order | Set 3 (Tree given as array)

Given a Complete Binary Tree as an array, the task is to print all of its levels in sorted order.

**Examples:**

Input:arr[] = {7, 6, 5, 4, 3, 2, 1} The given tree looks like 7 / \ 6 5 / \ / \ 4 3 2 1Output:7 5 6 1 2 3 4Input:arr[] = {5, 6, 4, 9, 2, 1} The given tree looks like 5 / \ 6 4 / \ / 9 2 1Output:5 4 6 1 2 9

**Approach:** A similar problem is discussed here

As the given tree is a Complete Binary Tree:

No. of nodes at a level l will be 2^{l}where l ≥ 0

- Start traversing the array with level initialized as 0.
- Sort the elements which are the part of the current level and print the elements.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print all the levels ` `// of the given tree in sorted order ` `void` `printSortedLevels(` `int` `arr[], ` `int` `n) ` `{ ` ` ` ` ` `// Initialize level with 0 ` ` ` `int` `level = 0; ` ` ` `for` `(` `int` `i = 0; i < n; level++) { ` ` ` ` ` `// Number of nodes at current level ` ` ` `int` `cnt = (` `int` `)` `pow` `(2, level); ` ` ` ` ` `// Indexing of array starts from 0 ` ` ` `// so subtract no. of nodes by 1 ` ` ` `cnt -= 1; ` ` ` ` ` `// Index of the last node in the current level ` ` ` `int` `j = min(i + cnt, n - 1); ` ` ` ` ` `// Sort the nodes of the current level ` ` ` `sort(arr + i, arr + j + 1); ` ` ` ` ` `// Print the sorted nodes ` ` ` `while` `(i <= j) { ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` `i++; ` ` ` `} ` ` ` `cout << endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 6, 4, 9, 2, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `printSortedLevels(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5 4 6 1 2 9

## Recommended Posts:

- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Binary Tree levels in sorted order
- Print even positioned nodes of even levels in level order of the given binary tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print a Binary Tree in Vertical Order | Set 1
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Print extreme nodes of each level of Binary Tree in alternate order
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print the nodes at odd levels of a tree
- Averages of Levels in Binary Tree
- Construct a complete binary tree from given array in level order fashion
- Check if the given array can represent Level Order Traversal of Binary Search 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.