C++ program to check whether a binary tree is a full binary tree or not using recursion
Description:
This Program is to check whether a binary tree is a full binary tree or not using recursion
For Example:
Input :
The Elements are : 1 ,2 ,3 ,4 ,5
Output :
The Binary Tree is full
Program :
//C++ program to check whether a binary tree is a full binary tree or not using recursion#include<bits/stdc++.h>usingnamespacestd;
// tree nodestructNode
{int data;
Node *left, *right;
};
// returns a new tree NodeNode* newNode(int data){
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
boolisTreeFull(struct Node* root){
// If empty treeif (root == NULL)
returntrue;
// If leaf nodeif (root->left == NULL && root->right == NULL)
returntrue;
// If both left and right are not NULLif ((root->left) && (root->right))
return (isTreeFull(root->left) && isTreeFull(root->right));
// elsereturnfalse;
}
intmain(){
structNode *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
if (isTreeFull(root))
cout<<"The Binary Tree is full";
elsecout<<"The Binary Tree is not full";
getchar();
return0;
}
Comments