
- 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
C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
Given a Binary Tree, the task is to check whether it is a Full Binary Tree or not. A Binary Tree is said to be a Full Binary Tree if every node has zero or two children.
For Example
Input-1
Output:
1
Explanation: Every node except the leaf node has two children, so it is a full binary tree.
Input-2:
Output:
0
Explanation: Node 2 has only one child, so it is not a full binary tree.
Approach to Solve this Problem
To check whether a given binary tree is full or not, we can check recursively for the left subtree and right subtree.
- Input a given Binary Tree having nodes and its children.
- A Boolean function isFullBinaryTree(Node*root) takes the root node as the input and returns True if it is full binary tree, otherwise false.
- In the base condition, if the root node is NULL or empty, then return True.
- If the left subtree and the right subtree is NULL or empty, then return True.
- Now let us check recursively for each of the left subtree and right subtree and return the output.
Example
#include<iostream> using namespace std; struct treenode { int data; treenode * left; treenode * right; }; struct treenode * createNode(int d) { struct treenode * root = new treenode; root -> data = d; root -> left = NULL; root -> right = NULL; return root; } bool isFullBinaryTree(struct treenode * root) { if (root == NULL) { return true; } if (root -> left == NULL and root -> right == NULL) { return true; } else if (root -> left and root -> right) { return (isFullBinaryTree(root -> left) and isFullBinaryTree(root -> right)); } return false; } int main() { struct treenode * root = NULL; root = createNode(1); root -> left = createNode(2); root -> right = createNode(3); root -> left -> right = createNode(4); root -> left -> left = createNode(5); root -> right -> left = createNode(6); if (isFullBinaryTree(root)) { cout << "1" << endl; } else { cout << "0" << endl; } return 0; }
Running the above code will generate the output as,
Output
0
Explanation: Since all the leaf nodes in the given binary tree do not have children nodes, it is not a Full Binary Tree. So, we get the output as 0.
- Related Articles
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- A program to check if a binary tree is BST or not in C ?
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- Program to check whether given tree is symmetric tree or not in Python
- Check if a binary tree is sorted levelwise or not in C++
- Check if a binary tree is subtree of another binary tree in C++
- C++ Program to Check if a Binary Tree is a BST
- Check if a binary tree is sorted level-wise or not in C++
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is Heap in C++
- How to check whether a binary tree has the given path sum in C#?

Advertisements