Print all internal nodes of a Binary tree

Given a Binary tree, the task is to print all the internal nodes in a tree.

An internal node is a node which carries at least one child or in other words, an internal node is not a leaf node. Here we intend to print all such internal nodes in level order. Consider the following Binary Tree:

Input:

Output: 15 10 20



The way to solve this involves a BFS of the tree. The algorithm is as follows:

  • Do a level order traversal by pushing nodes in the queue one by one.
  • Pop the elements from the queue one by one and keep a track of following cases:
    1. The node has a left child only.
    2. The node has a right child only.
    3. The node has both left and right child.
    4. The node has no children at all.
  • Except for case 4, print the data in the node for all the other 3 cases.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print all internal
// nodes in tree
#include <bits/stdc++.h>
using namespace std;
  
// A node in the Binary tree
struct Node {
    int data;
    Node *left, *right;
    Node(int data)
    {
       left = right = NULL;
       this->data = data;
    }
};
  
// Function to print all internal nodes 
// in level order from left to right
void printInternalNodes(Node* root)
{
    // Using a queue for a level order traversal
    queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
  
        // Check and pop the element in 
        // the front of the queue
        Node* curr = q.front();
        q.pop();
  
        // The variable flag keeps track of 
        // whether a node is an internal node
        bool isInternal = 0;
  
        // The node has a left child
        if (curr->left) {
            isInternal = 1;
            q.push(curr->left);
        }
  
        // The node has a right child
        if (curr->right) {
            isInternal = 1;
            q.push(curr->right);
        }
  
        // In case the node has either a left 
        // or right child or both print the data
        if (isInternal)
            cout << curr->data << " ";
    }
}
  
// Driver program to build a sample tree
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->right->left = new Node(5);
    root->right->right = new Node(6);
    root->right->right->right = new Node(10);
    root->right->right->left = new Node(7);
    root->right->left->left = new Node(8);
    root->right->left->right = new Node(9);
  
    // A call to the function
    printInternalNodes(root);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all internal 
// nodes in tree
import java.util.*;
class GfG 
{
  
// A node in the Binary tree 
static class Node 
    int data; 
    Node left, right; 
    Node(int data) 
    
        left = right = null
        this.data = data; 
    
}
  
// Function to print all internal nodes 
// in level order from left to right 
static void printInternalNodes(Node root) 
    // Using a queue for a level order traversal 
    Queue<Node> q = new LinkedList<Node>(); 
    q.add(root); 
    while (!q.isEmpty()) 
    
  
        // Check and pop the element in 
        // the front of the queue 
        Node curr = q.peek(); 
        q.remove(); 
  
        // The variable flag keeps track of 
        // whether a node is an internal node 
        boolean isInternal = false
  
        // The node has a left child 
        if (curr.left != null
        
            isInternal = true
            q.add(curr.left); 
        
  
        // The node has a right child 
        if (curr.right != null
        
            isInternal = true
            q.add(curr.right); 
        
  
        // In case the node has either a left 
        // or right child or both print the data 
        if (isInternal == true
            System.out.print(curr.data + " "); 
    
  
// Driver code
public static void main(String[] args) 
    Node root = new Node(1); 
    root.left = new Node(2); 
    root.right = new Node(3); 
    root.left.left = new Node(4); 
    root.right.left = new Node(5); 
    root.right.right = new Node(6); 
    root.right.right.right = new Node(10); 
    root.right.right.left = new Node(7); 
    root.right.left.left = new Node(8); 
    root.right.left.right = new Node(9); 
  
    // A call to the function 
    printInternalNodes(root); 
}
  
// This code is contributed by 
// Prerna Saini.

chevron_right


Output:

1 2 3 5 6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : prerna saini