Print all internal nodes of a Binary tree
Last Updated :
28 Jun, 2021
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:
- The node has a left child only.
- The node has a right child only.
- The node has both left and right child.
- 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node( int data)
{
left = right = NULL;
this ->data = data;
}
};
void printInternalNodes(Node* root)
{
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* curr = q.front();
q.pop();
bool isInternal = 0;
if (curr->left) {
isInternal = 1;
q.push(curr->left);
}
if (curr->right) {
isInternal = 1;
q.push(curr->right);
}
if (isInternal)
cout << curr->data << " " ;
}
}
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);
printInternalNodes(root);
return 0;
}
|
Java
import java.util.*;
class GfG
{
static class Node
{
int data;
Node left, right;
Node( int data)
{
left = right = null ;
this .data = data;
}
}
static void printInternalNodes(Node root)
{
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty())
{
Node curr = q.peek();
q.remove();
boolean isInternal = false ;
if (curr.left != null )
{
isInternal = true ;
q.add(curr.left);
}
if (curr.right != null )
{
isInternal = true ;
q.add(curr.right);
}
if (isInternal == true )
System.out.print(curr.data + " " );
}
}
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 );
printInternalNodes(root);
}
}
|
Python3
class new_Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def printInternalNodes(root):
q = []
q.append(root)
while ( len (q)):
curr = q[ 0 ]
q.pop( 0 )
isInternal = 0
if (curr.left):
isInternal = 1
q.append(curr.left)
if (curr.right):
isInternal = 1
q.append(curr.right)
if (isInternal):
print (curr.data, end = " " )
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 )
printInternalNodes(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
left = right = null ;
this .data = data;
}
}
static void printInternalNodes(Node root)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count != 0)
{
Node curr = q.Peek();
q.Dequeue();
Boolean isInternal = false ;
if (curr.left != null )
{
isInternal = true ;
q.Enqueue(curr.left);
}
if (curr.right != null )
{
isInternal = true ;
q.Enqueue(curr.right);
}
if (isInternal == true )
Console.Write(curr.data + " " );
}
}
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);
printInternalNodes(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function printInternalNodes(root)
{
let q = [];
q.push(root);
while (q.length > 0)
{
let curr = q[0];
q.shift();
let isInternal = false ;
if (curr.left != null )
{
isInternal = true ;
q.push(curr.left);
}
if (curr.right != null )
{
isInternal = true ;
q.push(curr.right);
}
if (isInternal == true )
document.write(curr.data + " " );
}
}
let 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);
printInternalNodes(root);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...