
- 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
Count Non-Leaf nodes in a Binary Tree in C++
We are given with a binary tree and the task is to calculate the count of non-leaf nodes available in a binary tree.
Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in linked list. Non-leaf nodes are also known as parent nodes as they have more than 0 child and less than two children.
Structure of a binary tree is given below −
For example
Input −
Output − count of non-leaf nodes is: 3
Explanation − In the given tree, we have 27, 14 and 35 as non-leaf nodes since they have more than 0 children and less than 2 children.
Approach used in the below program is as follows
Create the structure of a binary tree containing, pointer to a left node, pointer to a right node and a data part stored in a node
Create a function that will insert a node whenever this function is called. For that, insert data in a new node and also set the right and left pointer of a new node to a NULL and return the node.
Create a recursive function that will count the number of non-leaf nodes in a binary tree.
- Check If root is NULL or left of root is NULL and right of root is NULL then return 0
- Return 1 + recursive call to this function with left pointer + recursive call to this function with right pointer
Print the count
Example
#include <iostream> using namespace std; // Node's structure struct Node { int data; struct Node* left; struct Node* right; }; // To define the new node struct Node* newNode(int data){ struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Count the non leaf nodes. int nonleaf(struct Node* root){ if (root == NULL || (root->left == NULL && root->right == NULL)){ return 0; } return 1 + nonleaf(root->left) + nonleaf(root->right); } // Main function int main(){ struct Node* root = newNode(10); root->left = newNode(21); root->right = newNode(33); root->left->left = newNode(48); root->left->right = newNode(51); cout << nonleaf(root); return 0; }
Output
If we run the above code it will generate the following output −
count of non-leaf nodes is: 2
- Related Articles
- Program to find leaf and non-leaf nodes of a binary tree in Python
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Python Program to Count Number of Non Leaf Nodes of a given Tree
- Product of all leaf nodes of binary tree in C++
- Golang Program to Count number of leaf nodes in a tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Count Good Nodes in Binary Tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Find height of a special binary tree whose leaf nodes are connected in C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Count half nodes in a Binary tree (Iterative and Recursive) in C++
- Count full nodes in a Binary tree (Iterative and Recursive) in C++
- Print leaf nodes in binary tree from left to right using one stack in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
