Given a Binary Tree, check whether it is a full binary tree or not. A full binary tree is a binary tree with either zero or two child nodes for each node.
Note: An empty tree (NULL node) is considered a full binary tree. Leaf Node is also considered a full binary tree.
Example :
Input: root = [1, 2, 3, 4, 5]
Output: true Explanation: Every node except leaf node has two children so it is a full tree.
Input: root = [1, 2, 3, 4]
Output: false Explanation: Node 2 has only one child so this is not a full tree.
Using Recursive Approach - O(n) Time and O(n) Space
The idea is to recursively check every node in the binary tree. So, for every node check if it is a leaf node, then return true else check if both left and right children exist, recursively check both subtrees and if exactly one child exists, then return false because the tree is not full.
If the current node is NULL, return true.
If the current node is a leaf node, return true.
Check whether both left and right children exist. If both children exist, recursively check the left and right subtrees.
If only one child exists, return false.
C++
#include<iostream>usingnamespacestd;// Definition of Binary Tree NodeclassNode{public:intdata;Node*left;Node*right;// ConstructorNode(intval){data=val;left=right=nullptr;}};// Function to check whether a binary tree is a Full Binary TreeboolisFullTree(Node*root){// Case 1: Empty tree is considered fullif(root==nullptr){returntrue;}// Case 2: Leaf nodeif(root->left==nullptr&&root->right==nullptr){returntrue;}// Case 3: Both children existif(root->left!=nullptr&&root->right!=nullptr){returnisFullTree(root->left)&&isFullTree(root->right);}// Case 4: One child is missingreturnfalse;}intmain(){/* 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree */Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);if(isFullTree(root)==true){cout<<"True";}else{cout<<"False";}return0;}
C
#include<stdbool.h>#include<stdio.h>#include<stdlib.h>// Definition of Binary Tree NodestructNode{intdata;structNode*left;structNode*right;};// Function to create a new nodestructNode*createNode(intval){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=val;newNode->left=NULL;newNode->right=NULL;returnnewNode;}// Function to check whether a binary tree is a Full Binary TreeboolisFullTree(structNode*root){// Case 1: Empty tree is considered fullif(root==NULL){returntrue;}// Case 2: Leaf nodeif(root->left==NULL&&root->right==NULL){returntrue;}// Case 3: Both children existif(root->left!=NULL&&root->right!=NULL){returnisFullTree(root->left)&&isFullTree(root->right);}// Case 4: One child is missingreturnfalse;}intmain(){/* 1 / \ 2 3 / \ 4 5 */structNode*root=createNode(1);root->left=createNode(2);root->right=createNode(3);root->left->left=createNode(4);root->left->right=createNode(5);if(isFullTree(root)){printf("True");}else{printf("False");}return0;}
Java
classNode{intdata;Nodeleft;Noderight;// ConstructorNode(intval){data=val;left=right=null;}}publicclassGFG{// Function to check whether a binary tree is a Full// Binary TreestaticbooleanisFullTree(Noderoot){// Case 1: Empty tree is considered fullif(root==null){returntrue;}// Case 2: Leaf nodeif(root.left==null&&root.right==null){returntrue;}// Case 3: Both children existif(root.left!=null&&root.right!=null){returnisFullTree(root.left)&&isFullTree(root.right);}// Case 4: One child is missingreturnfalse;}publicstaticvoidmain(String[]args){/* 1 / \ 2 3 / \ 4 5 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){System.out.println("True");}else{System.out.println("False");}}}
Python
# Definition of Binary Tree NodeclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=None# Function to check whether a binary tree is a Full Binary TreedefisFullTree(root):# Case 1: Empty tree is considered fullifrootisNone:returnTrue# Case 2: Leaf nodeifroot.leftisNoneandroot.rightisNone:returnTrue# Case 3: Both children existifroot.leftisnotNoneandroot.rightisnotNone:returnisFullTree(root.left)andisFullTree(root.right)# Case 4: One child is missingreturnFalse# Driver Codeif__name__=="__main__":''' 1 / \ 2 3 / \ 4 5 '''root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)ifisFullTree(root):print("True")else:print("False")
C#
usingSystem;// Definition of Binary Tree NodeclassNode{publicintdata;publicNodeleft;publicNoderight;// ConstructorpublicNode(intval){data=val;left=right=null;}}classGFG{// Function to check whether a binary tree is a Full// Binary TreestaticboolisFullTree(Noderoot){// Case 1: Empty tree is considered fullif(root==null){returntrue;}// Case 2: Leaf nodeif(root.left==null&&root.right==null){returntrue;}// Case 3: Both children existif(root.left!=null&&root.right!=null){returnisFullTree(root.left)&&isFullTree(root.right);}// Case 4: One child is missingreturnfalse;}staticvoidMain(){/* 1 / \ 2 3 / \ 4 5 */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){Console.WriteLine("True");}else{Console.WriteLine("False");}}}
JavaScript
// Definition of Binary Tree NodeclassNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function to check whether a binary tree is a Full Binary// TreefunctionisFullTree(root){// Case 1: Empty tree is considered fullif(root===null){returntrue;}// Case 2: Leaf nodeif(root.left===null&&root.right===null){returntrue;}// Case 3: Both children existif(root.left!==null&&root.right!==null){returnisFullTree(root.left)&&isFullTree(root.right);}// Case 4: One child is missingreturnfalse;}// Driver Code/* 1 / \ 2 3 / \ 4 5*/letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){console.log("True");}else{console.log("False");}
Output
True
Using Level Order Traversal - O(n) Time and O(n) Space
The idea is to perform a level order traversal using a queue to visit every node of the binary tree. For each node, we check whether it has either 0 children or 2 children. If both children exist, we insert them into the queue for further checking. If exactly one child exists, the tree cannot be a Full Binary Tree, so we return false. If all nodes satisfy the condition, we return true.
If the root is NULL, return true.
Create a queue and insert the root node into it.
Traverse the tree while the queue is not empty.
Remove the front node from the queue.
If the node has both left and right children, insert them into the queue.
If the node has exactly one child, return false; otherwise return true after traversal ends.
C++
#include<iostream>#include<queue>usingnamespacestd;// Definition of Binary Tree NodeclassNode{public:intdata;Node*left;Node*right;// ConstructorNode(intval){data=val;left=right=nullptr;}};// Function to check whether a binary tree is a Full Binary TreeboolisFullTree(Node*root){// Empty tree is considered fullif(root==nullptr){returntrue;}// Queue for level order traversalqueue<Node*>q;// Insert root node into queueq.push(root);// Traverse until queue becomes emptywhile(!q.empty()){// Get front nodeNode*curr=q.front();q.pop();// Leaf nodeif(curr->left==nullptr&&curr->right==nullptr){continue;}// If both children existif(curr->left!=nullptr&&curr->right!=nullptr){q.push(curr->left);q.push(curr->right);}else{// One child is missingreturnfalse;}}// Tree is fullreturntrue;}intmain(){/* 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree */Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);if(isFullTree(root)==true){cout<<"True";}else{cout<<"False";}return0;}
Java
importjava.util.LinkedList;importjava.util.Queue;// Definition of Binary Tree NodeclassNode{intdata;Nodeleft;Noderight;// ConstructorNode(intval){data=val;left=right=null;}}publicclassGFG{// Function to check whether a binary tree is a Full// Binary TreestaticbooleanisFullTree(Noderoot){// Empty tree is considered fullif(root==null){returntrue;}// Queue for level order traversalQueue<Node>q=newLinkedList<>();// Insert root node into queueq.offer(root);// Traverse until queue becomes emptywhile(!q.isEmpty()){// Get front nodeNodecurr=q.poll();// Leaf nodeif(curr.left==null&&curr.right==null){continue;}// If both children existif(curr.left!=null&&curr.right!=null){q.offer(curr.left);q.offer(curr.right);}else{// One child is missingreturnfalse;}}// Tree is fullreturntrue;}publicstaticvoidmain(String[]args){/* 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){System.out.println("True");}else{System.out.println("False");}}}
Python
fromcollectionsimportdeque# Definition of Binary Tree NodeclassNode:def__init__(self,val):self.data=valself.left=Noneself.right=None# Function to check whether a binary tree is a Full Binary TreedefisFullTree(root):# Empty tree is considered fullifrootisNone:returnTrue# Queue for level order traversalq=deque()# Insert root node into queueq.append(root)# Traverse until queue becomes emptywhileq:# Get front nodecurr=q.popleft()# Leaf nodeifcurr.leftisNoneandcurr.rightisNone:continue# If both children existifcurr.leftisnotNoneandcurr.rightisnotNone:q.append(curr.left)q.append(curr.right)else:# One child is missingreturnFalse# Tree is fullreturnTrue# Driver Codeif__name__=="__main__":''' 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree '''root=Node(1)root.left=Node(2)root.right=Node(3)root.left.left=Node(4)root.left.right=Node(5)ifisFullTree(root):print("True")else:print("False")
C#
usingSystem;usingSystem.Collections.Generic;// Definition of Binary Tree NodeclassNode{publicintdata;publicNodeleft;publicNoderight;// ConstructorpublicNode(intval){data=val;left=right=null;}}classGFG{// Function to check whether a binary tree is a Full// Binary TreestaticboolisFullTree(Noderoot){// Empty tree is considered fullif(root==null){returntrue;}// Queue for level order traversalQueue<Node>q=newQueue<Node>();// Insert root node into queueq.Enqueue(root);// Traverse until queue becomes emptywhile(q.Count>0){// Get front nodeNodecurr=q.Dequeue();// Leaf nodeif(curr.left==null&&curr.right==null){continue;}// If both children existif(curr.left!=null&&curr.right!=null){q.Enqueue(curr.left);q.Enqueue(curr.right);}else{// One child is missingreturnfalse;}}// Tree is fullreturntrue;}staticvoidMain(){/* 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree */Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){Console.WriteLine("True");}else{Console.WriteLine("False");}}}
JavaScript
// Definition of Binary Tree NodeclassNode{constructor(val){this.data=val;this.left=null;this.right=null;}}// Function to check whether a binary tree is a Full Binary// TreefunctionisFullTree(root){// Empty tree is considered fullif(root===null){returntrue;}// Queue for level order traversalletq=[];// Insert root node into queueq.push(root);// Traverse until queue becomes emptywhile(q.length>0){// Get front nodeletcurr=q.shift();// Leaf nodeif(curr.left===null&&curr.right===null){continue;}// If both children existif(curr.left!==null&&curr.right!==null){q.push(curr.left);q.push(curr.right);}else{// One child is missingreturnfalse;}}// Tree is fullreturntrue;}// Driver Code/* 1 / \ 2 3 / \ 4 5 This is a Full Binary Tree*/letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);if(isFullTree(root)){console.log("True");}else{console.log("False");}