
- 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 full nodes in a Binary Tree in C++
In this problem, we are given a binary tree. Our task is to print all nodes of the tree that are full nodes.
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 −
A full node is a node that has both its left and right child available. In other words, a node with the left and right child is a full node. In the above binary tree, 4 and 9 are full nodes.
Let’s take an example to understand the problem −
Output − 4 9
A simple and easy approach to solve this problem is to traverse the tree using any traversal algorithm. Check if the current node has left and right children or node. If yes then print the node’s value otherwise leave it.
Example
Program to illustrate our solution,
#include <iostream> using namespace std; struct Node{ int data; struct Node *left, *right; }; Node *insertNode(int data){ Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } void printFullNode(Node *root){ if (root != NULL){ printFullNode(root->left); if (root->left != NULL && root->right != NULL) cout<<root->data<<"\t"; printFullNode(root->right); } } int main(){ Node* root = insertNode(100); root->left = insertNode(56); root->right = insertNode(12); root->left->left = insertNode(89); root->right->left = insertNode(32); root->right->right = insertNode(45); cout<<"All full nodes of the tree are :\n"; printFullNode(root); return 0; }
Output
All full nodes of the tree are − 100 12
- Related Articles
- Print all internal nodes of 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 nodes between two given levels in Binary Tree in C++
- Count full nodes in a Binary tree (Iterative and Recursive) in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Product of all nodes in a Binary Tree in C++
- All Nodes Distance K in Binary Tree in C++
- Program to print path from root to all nodes in a Complete Binary Tree using 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++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Product of all leaf nodes of binary tree in C++
