C++ program to print right view of a Binary tree using queue














































C++ program to print right view of a Binary tree using queue



Program:
#include <iostream>
#include <list>
using namespace std;
 
// Data structure to store a binary tree node
struct Node
{
    int key;
    Node *left, *right;
 
    Node(int key)
    {
        this->key = key;
        this->left = this->right = nullptr;
    }
};
 
// Iterative function to print the right view of a given binary tree
void printRightView(Node* root)
{
    // return if the tree is empty
    if (root == nullptr) {
        return;
    }
 
    // create an empty queue and enqueue the root node
    list<Node*> queue;
    queue.push_back(root);
 
    // pointer to store the current node
    Node* curr = nullptr;
 
    // loop till queue is empty
    while (!queue.empty())
    {
        // calculate the total number of nodes at the current level
        int size = queue.size();
        int i = 0;
 
        // process every node of the current level and enqueue their
        // non-empty right and right child
        while (i++ < size)
        {
            curr = queue.front();
            queue.pop_front();
 
            // if this is the last node of the current level, print it
            if (i == size) {
                cout << curr->key << " ";
            }
 
            if (curr->left) {
                queue.push_back(curr->left);
            }
 
            if (curr->right) {
                queue.push_back(curr->right);
            }
        }
    }
}
 
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->right = new Node(4);
    root->right->left = new Node(5);
    root->right->right = new Node(6);
    root->right->left->left = new Node(7);
    root->right->left->right = new Node(8);
 
    printRightView(root);
 
    return 0;
}
Output:
1 3 6 8




More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 394 1
C++ program to Sort a Linked List Using Merge Sort 358 1
C++ Program to Implement a Linked List representation of a Binary tree 353 1
C++ Program to Check for balanced parentheses in an expression 251 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 289 1
C++ program to print Indian flag 385 1
C++ program to Convert a multi level linked list to a singly linked list 269 1
C++ program to print right view of a Binary tree using queue 244 1
C++ Program to implement Huffman Coding Compression Algorithm 1660 1
C++ Program to Create a Height Balanced Binary Tree 274 1
C++ program to implement Prims algorithm 635 1
C++ Program for BFS Traversal 289 1
C++ Progam to Evaluate a Prefix Expression 460 1
C++ Program to Implement Queue using Linked List 254 1
C++ implementation of Stack using Linked list 303 1
C++ program to find the intersection point of two linked lists 340 1
C++ program to count the inversions in the given array 278 1
C++ program to perform D.N.F sort 325 1
C++ program to print all possible subsets of a String 286 1
C++ program to count the number of ones in a binary representation of a number 310 1
C++ program to print all possible subsets of a set 322 1
C++ program to find the largest word in a String 287 1
C++ Program to print a matrix in Spiral order 365 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 313 1
C limits library 340 1
Program to add two Binary numbers 332 1

Comments