Print all Nodes Except Rightmost Node of Every Level of the Binary Tree


In this problem, we will print all the nodes of the binary tree except the rightmost node of each level.

We will use the level order traversal to traverse the binary tree, and we won’t print the last node of each level, which is the rightmost node.

Problem statement − We have given a binary tree containing different nodes. We need to print all nodes of the binary tree except right most node.

Sample examples

Input 

          7
       /     \
      8       9
    /   \       \
   2     7       21
        /  \
       21    43

Output

8
2  7
21

Explanation : We have printed all nodes except the rightmost node of each level.

Input

             35
           /    \    
         74     21    
        /  \    / \       
      32    15 90  576        
     /  \
    45  83

Output 

74
32 15 90
45

Explanation − Here, we print all nodes except the rightmost node.

Approach 1

In this approach, we will use the queue to perform the level order traversal on the given binary tree. In level order traversal, we won’t print the last node of each level to exclude the rightmost node from each level.

Algorithm

Step 1− Create a TreeNode structure for the binary tree node. The tree node contains the ‘val’ to store the data and the ‘left’ and ‘right’ pointers to track the left and right child nodes of the particular node.

Step 2 − After that, define the createNode() function to create a new node.

Step 2.1 − Create a new ‘tmp’ node, and initialize its values with the given value. Also, initialize the left and right pointer with NULL.

Step 2.2 − Return the ‘tmp’ node from the function.

Step 3 − In the main() method, create a binary tree by adding nodes.

Step 4 − Execute the printNodes() function to print each binary tree node except the rightmost node of the binary tree.

Step 5 − In the printNodes() function, if the head pointer is NULL, execute the return statement.

Step 6 − Define the queue to store the tree nodes and insert the head node.

Step 7 − Make iterations using the while loop. Store the queue size in the ‘totalNodes’ variable in the while loop.

Step 8 − If ‘totalNodes’ is 0, break the loop.

Step 9 − Use nested while loop to traverse each level.

Step 10 − Pop the first element from the queue, and print it if ‘totalNodes’ is not equal to 1.

Step 11 − If the current node's left child exists, push it to the queue.

Step 12 − If the right node of the current node exists, push it to the queue.

Step 13 − Decrement ‘totalNodes’ by 1.

Example

#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
    int val;
    struct TreeNode *left, *right;
};
TreeNode *createNewNode(int val) {
    TreeNode *tmp = new TreeNode;
    tmp->val = val;
    tmp->left = NULL;
    tmp->right = NULL;
    return (tmp);
}
void printNodes(TreeNode *head) {
    if (head == NULL)
        return;
    queue<TreeNode *> que;
    que.push(head);
    while (true) {
        int totalNodes = que.size();
        if (totalNodes == 0)
            break;
        while (totalNodes > 0) {
            TreeNode *node = que.front();
            // If node is not rightmost print
            if (totalNodes != 1)
                cout << node->val << " ";
            que.pop();
            // Insert the left and right node of the current node in the queue
            if (node->left != NULL)
                que.push(node->left);
            if (node->right != NULL)
                que.push(node->right);
            totalNodes--;
        }
        cout << "\n";
    }
}
int main() {
    TreeNode *head = createNewNode(35);
    head->left = createNewNode(74);
    head->right = createNewNode(21); 
    head->right->left = createNewNode(90);
    head->right->right = createNewNode(576);
    head->left->left = createNewNode(32);
    head->left->right = createNewNode(15);
    head->left->left->left = createNewNode(45);
    head->left->right->right = createNewNode(83);
    printNodes(head);
    return 0;
}

Output

74 
32 15 90 
45

Time complexity − O(N), where N is the total number of nodes in the binary tree.

Space complexity − O(M), where M is the maximum number of nodes in one level.

We learned to print each binary tree node except the rightmost node using the level order traversal. However, programmers can write code to print each node of the binary tree except the leftmost node. In which programmers need to print all nodes except the first node of the queue.

Updated on: 22-Jul-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements