
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print Binary Tree levels in sorted order in C++
In this problem, we are given a binary tree and we have to print all nodes at a level in sorted order of their values.
Let’s take an example to understand the concept better,
Input −
Output −
20 6 15 2 17 32 78
To solve this problem, we need to print a sorted order of each level of the tree. For this, we need to create a queue and two priority queues. The NULL separator is used to separate two levels.
Example
Program to illustrate the logic −
#include <iostream> #include <queue> #include <vector> using namespace std; struct Node { int data; struct Node *left, *right; }; void printLevelElements(Node* root){ if (root == NULL) return; queue<Node*> q; priority_queue<int, vector<int>, greater<int> > current_level; priority_queue<int, vector<int>, greater<int> > next_level; q.push(root); q.push(NULL); current_level.push(root->data); while (q.empty() == false) { int data = current_level.top(); Node* node = q.front(); if (node == NULL) { q.pop(); if (q.empty()) break; q.push(NULL); cout << "\n"; current_level.swap(next_level); continue; } cout << data << " "; q.pop(); current_level.pop(); if (node->left != NULL) { q.push(node->left); next_level.push(node->left->data); } if (node->right != NULL) { q.push(node->right); next_level.push(node->right->data); } } } Node* insertNode(int data){ Node* temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } int main(){ Node* root = insertNode(12); root->left = insertNode(98); root->right = insertNode(34); root->left->left = insertNode(76); root->left->right = insertNode(5); root->right->left = insertNode(12); root->right->right = insertNode(45); cout << "Elements at each Level of binary tree are \n"; printLevelElements(root); return 0; }
Output
Elements at each Level of binary tree are 12 34 98 5 12 45 76
- Related Articles
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- Print all nodes between two given levels in Binary Tree in C++
- Average of Levels in Binary Tree in C++
- Print Binary Tree in C++
- Binary Tree Level Order Traversal in C++
- Binary Tree Vertical Order Traversal in C++
- Print all permutations in sorted (lexicographic) order in C++
- Print Binary Tree in 2-Dimensions in C++
- Convert Sorted List to Binary Search Tree in C++
- Find the n-th binary string in sorted order in C++
- Print all full nodes in a Binary Tree in C++
- Print the nodes at odd levels of a tree in C++ Programming.
- Print Left View of a Binary Tree in C language
- Print Right View of a Binary Tree in C language
- Print all odd nodes of Binary Search Tree in C++

Advertisements