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:
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# program to print all internal
// nodes in tree
// A node in the Binary tree
public class Node
public int data;
public Node left, right;
public 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
while (q.Count != 0)
// Check and pop the element in
// the front of the queue
Node curr = q.Peek();
// 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;
// The node has a right child
if (curr.right != null)
isInternal = true;
// In case the node has either a left
// or right child or both print the data
if (isInternal == true)
Console.Write(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
// This code contributed by Rajput-Ji
1 2 3 5 6
- Print path between any two nodes in a Binary Tree
- Print all full nodes in a Binary Tree
- Print Nodes in Top View of Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print all nodes between two given levels in Binary Tree
- Print all even nodes of Binary Search Tree
- Print all nodes in a binary tree having K leaves
- Print all odd nodes of Binary Search Tree
- Print nodes in top view of Binary Tree | Set 2
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print all leaf nodes of a binary tree from right to left
- Print the nodes of binary tree as they become the leaf node
- Print leftmost and rightmost nodes of a Binary Tree
- Print nodes between two given level numbers of a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.