How to Print Data in Binary Tree Level by Level in C++? Last Updated : 17 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A binary tree is a non-linear hierarchical data structure where each node can have at most two children which are termed as left and right child. In this article, we will learn how to print data in a binary tree level by level in C++. Example Input: 10 / \ 20 30 / \ / \40 50 60 70Output:10 20 30 40 50 60 70Printing Data of a Binary Tree Level by Level in C++The method of printing the data of a binary tree level by level is also known as level order traversal where data of a binary tree is printed in top to bottom order from left to right at each level of the binary tree. To print the data of a binary tree level by level we can follow the below approach: Level Order TraversalApproachInitialize an empty queue q.Enqueue the root node into the queue.While q is not empty:Get the current size of the queue and store it in a variable n.Run a for loop for n times: Dequeue a node from the queue.Print the data of the node.Enqueue node->left into the queue if it exists.Enqueue node->right into the queue if it exists.Print a new line after processing all nodes of the binary tree at the current level.C++ Program to Print Data in a Binary Tree Level by Level The following program illustrates how we can print the data of a binary tree level by level in C++: C++ // C++ Program to Print Data in Binary Tree Level by Level #include <iostream> #include <queue> using namespace std; // Class for a Node in the binary tree class Node { public: int data; Node* left; Node* right; // Constructor to initialize Node with given value Node(int value) { data = value; left = nullptr; right = nullptr; } }; // Class representing a Binary Tree class BinaryTree { private: // Pointer to the root node of the binary tree Node* root; public: // Constructor to initialize the binary tree with a given root node BinaryTree(Node* rootNode) : root(rootNode) {} // Function to perform level order traversal and print nodes level by level void printLevelOrder() { if (root == nullptr) return; // Queue to store nodes for level order traversal queue<Node*> q; // Enqueue the root node q.push(root); while (!q.empty()) { // Get the current level size int levelSize = q.size(); // Process all nodes at the current level for (int i = 0; i < levelSize; ++i) { Node* node = q.front(); cout << node->data << " "; q.pop(); // Enqueue left child if it exists if (node->left != nullptr) q.push(node->left); // Enqueue right child if it exists if (node->right != nullptr) q.push(node->right); } // Print new line after each level is processed cout << endl; } } }; int main() { // Create nodes for the binary tree Node* root = new Node(10); root->left = new Node(20); root->right = new Node(30); root->left->left = new Node(40); root->left->right = new Node(50); root->right->left = new Node(60); root->right->right = new Node(70); // Create a binary tree instance with the root node BinaryTree btree(root); // call the printlevel order function to print the data of binary tree level by level cout<<"Data of the Binary tree Level by Level:"<<endl; btree.printLevelOrder(); return 0; } OutputData of the Binary tree Level by Level: 10 20 30 40 50 60 70 Time Complexity: O(N), here denotes the total number of nodes in the binary tree.Auxiliary Space: O(N) We can also do it without using queue. To know more about level order traversal, refer to the article - Level Order Traversal (Breadth First Search or BFS) of Binary Tree Comment More infoAdvertise with us Next Article Print N-ary tree graphically G gpancomputer Follow Improve Article Tags : C++ Programs C++ CPP Examples CPP-DSA Practice Tags : CPP Similar Reads How to Read Binary Search Tree from File in C++? A binary search tree is a hierarchical data structure in which for every node in the tree, the value of all nodes in the left subtree is less than the node's value and the value of all nodes in the right subtree is greater than the node's value. This property of the binary search tree makes it effic 4 min read Print nodes in top view of Binary Tree | Set 2 Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes should be printed from left to right. Note: A node x is there in output if x is the topmost node at its horizontal distance. Horizontal distance 14 min read Inorder Tree Traversal of Binary Tree in C++ A binary tree is a non-linear hierarchical data structure in which each node has at most two children known as the left child and the right child. As the binary tree has non-linear structure it can be traversed in multiple ways one such way is in-order traversal which is a depth first (DFS) traversa 4 min read Print leaf nodes in binary tree from left to right using one stack Given a binary tree, the task is to print all leaf nodes of the given binary tree from left to right. That is, the nodes should be printed in the order they appear from left to right in the given tree.Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : 4 5 6 7 Input : 4 / \ 5 9 / \ / \ 8 3 7 2 / / 8 min read Print N-ary tree graphically Given an N-ary tree, the task is to print the N-ary tree graphically.Graphical Representation of Tree: A representation of tree in which the root is printed in a line and the children nodes are printed in subsequent lines with some amount of indentation. Examples: Input: 0 / | \ / | \ 1 2 3 / \ / | 11 min read Iterative Boundary Traversal of Complete Binary tree Given a complete binary tree, the task is to traverse it such that all the boundary nodes are visited in Anti-Clockwise order starting from the root.Example: Input: Output: 1 2 4 5 6 7 3 Input: Output: 18 15 40 50 100 20 30Approach:Traverse left-most nodes of the tree from top to down. (Left boundar 9 min read Like