
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all internal nodes of a Binary tree in C++
In this problem, we are given a binary tree and we have to print all internal nodes of the binary tree.
The binary tree is a tree in which a node can have a maximum of 2 child nodes. Node or vertex can have no nodes, one child or two child nodes.
Example −
Internal Node is a node that can have at least one child i.e. non-leaf node is an internal node.
Let’s take an example to understand the problem −
Output − 7 4 9
To solve this problem, we will traverse the binary tree using BFS(breadth-first search) traversal.
While traversal we will push nodes to a queue. When we pop elements from the queue, we will print all nodes of the tree that do not have any child nodes.
Example
Our logic is implemented by the below code −
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; Node(int data){ left = right = NULL; this->data = data; } }; void printNonLeafNodes(Node* root) { queue<Node*> treeNodes; treeNodes.push(root); while (!treeNodes.empty()) { Node* curr = treeNodes.front(); treeNodes.pop(); bool isInternal = 0; if (curr->left) { isInternal = 1; treeNodes.push(curr->left); } if (curr->right) { isInternal = 1; treeNodes.push(curr->right); } if (isInternal) cout<<curr->data<<"\t"; } } int main() { Node* root = new Node(43); root->left = new Node(12); root->right = new Node(78); root->left->left = new Node(4); root->right->left = new Node(9); root->right->right = new Node(1); root->right->right->right = new Node(50); root->right->right->left = new Node(25); cout<<"All internal Nodes of the binary tree are :\n"; printNonLeafNodes(root); return 0; }
Output
All internal Nodes of the binary tree are − 43 12 78 1
- Related Articles
- Print all full nodes in a Binary Tree in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- Print all nodes in a binary tree having K leaves in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Print all nodes between two given levels in Binary Tree in C++
- Product of all nodes in a Binary Tree in C++
- Print leftmost and rightmost nodes of a Binary Tree in C Program.
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Product of all leaf nodes of binary tree in C++
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- All Nodes Distance K in Binary Tree in C++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Python Program to Find the Sum of All Nodes in a Binary Tree

Advertisements