A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here.
For Example : 
 
To check whether a binary tree is a full binary tree we need to test the following cases:-
- If a binary tree node is NULL then it is a full binary tree.
- If a binary tree node does have empty left and right sub-trees, then it is a full binary tree by definition.
- If a binary tree node has left and right sub-trees, then it is a part of a full binary tree by definition. In this case recursively check if the left and right sub-trees are also binary trees themselves.
- In all other combinations of right and left sub-trees, the binary tree is not a full binary tree.
Following is the implementation for checking if a binary tree is a full binary tree.
C++
| // C++ program to check whether a given Binary Tree is full or not#include <bits/stdc++.h>usingnamespacestd; /*  Tree node structure */structNode{    intkey;    structNode *left, *right;}; /* Helper function that allocates a new node with the   given key and NULL left and right pointer. */structNode *newNode(chark){    structNode *node = newNode;    node->key = k;    node->right = node->left = NULL;    returnnode;} /* This function tests if a binary tree is a full binary tree. */boolisFullTree (structNode* root){    // If empty tree    if(root == NULL)        returntrue;     // If leaf node    if(root->left == NULL && root->right == NULL)        returntrue;     // If both left and right are not NULL, and left & right subtrees    // are full    if((root->left) && (root->right))        return(isFullTree(root->left) && isFullTree(root->right));     // We reach here when none of the above if conditions work    returnfalse;} // Driver Programintmain(){    structNode* root = NULL;    root = newNode(10);    root->left = newNode(20);    root->right = newNode(30);     root->left->right = newNode(40);    root->left->left = newNode(50);    root->right->left = newNode(60);    root->right->right = newNode(70);     root->left->left->left = newNode(80);    root->left->left->right = newNode(90);    root->left->right->left = newNode(80);    root->left->right->right = newNode(90);    root->right->left->left = newNode(80);    root->right->left->right = newNode(90);    root->right->right->left = newNode(80);    root->right->right->right = newNode(90);     if(isFullTree(root))        cout << "The Binary Tree is full\n";    else        cout << "The Binary Tree is not full\n";     return(0);}// This code is contributed by shubhamsingh10 | 
C
| // C program to check whether a given Binary Tree is full or not#include<stdio.h>#include<stdlib.h>#include<stdbool.h>/*  Tree node structure */structNode{    intkey;    structNode *left, *right;};/* Helper function that allocates a new node with the   given key and NULL left and right pointer. */structNode *newNode(chark){    structNode *node = (structNode*)malloc(sizeof(structNode));    node->key = k;    node->right = node->left = NULL;    returnnode;}/* This function tests if a binary tree is a full binary tree. */boolisFullTree (structNode* root){    // If empty tree    if(root == NULL)        returntrue;    // If leaf node    if(root->left == NULL && root->right == NULL)        returntrue;    // If both left and right are not NULL, and left & right subtrees    // are full    if((root->left) && (root->right))        return(isFullTree(root->left) && isFullTree(root->right));    // We reach here when none of the above if conditions work    returnfalse;}// Driver Programintmain(){    structNode* root = NULL;    root = newNode(10);    root->left = newNode(20);    root->right = newNode(30);    root->left->right = newNode(40);    root->left->left = newNode(50);    root->right->left = newNode(60);    root->right->right = newNode(70);    root->left->left->left = newNode(80);    root->left->left->right = newNode(90);    root->left->right->left = newNode(80);    root->left->right->right = newNode(90);    root->right->left->left = newNode(80);    root->right->left->right = newNode(90);    root->right->right->left = newNode(80);    root->right->right->right = newNode(90);    if(isFullTree(root))        printf("The Binary Tree is full\n");    else        printf("The Binary Tree is not full\n");    return(0);} | 
Java
| // Java program to check if binary tree is full or not/*  Tree node structure */classNode {    intdata;    Node left, right;     Node(intitem)     {        data = item;        left = right = null;    }} classBinaryTree {    Node root;         /* this function checks if a binary tree is full or not */    booleanisFullTree(Node node)    {        // if empty tree        if(node == null)        returntrue;                 // if leaf node        if(node.left == null&& node.right == null)            returntrue;                 // if both left and right subtrees are not null        // they are full        if((node.left!=null) && (node.right!=null))            return(isFullTree(node.left) && isFullTree(node.right));                 // if none work        returnfalse;    }          // Driver program    publicstaticvoidmain(String args[])     {        BinaryTree tree = newBinaryTree();        tree.root = newNode(10);        tree.root.left = newNode(20);        tree.root.right = newNode(30);        tree.root.left.right = newNode(40);        tree.root.left.left = newNode(50);        tree.root.right.left = newNode(60);        tree.root.left.left.left = newNode(80);        tree.root.right.right = newNode(70);        tree.root.left.left.right = newNode(90);        tree.root.left.right.left = newNode(80);        tree.root.left.right.right = newNode(90);        tree.root.right.left.left = newNode(80);        tree.root.right.left.right = newNode(90);        tree.root.right.right.left = newNode(80);        tree.root.right.right.right = newNode(90);                 if(tree.isFullTree(tree.root))            System.out.print("The binary tree is full");        else            System.out.print("The binary tree is not full");     }} // This code is contributed by Mayank Jaiswal  | 
Python3
| # Python program to check whether given Binary tree is full or not# Tree node structureclassNode:    # Constructor of the node class for creating the node    def__init__(self, key):        self.key =key        self.left =None        self.right =None# Checks if the binary tree is full or notdefisFullTree(root):    # If empty tree    ifroot isNone:            returnTrue        # If leaf node    ifroot.left isNoneandroot.right isNone:        returnTrue    # If both left and right subtress are not None and    # left and right subtress are full    ifroot.left isnotNoneandroot.right isnotNone:        return(isFullTree(root.left) andisFullTree(root.right))        # We reach here when none of the above if conditions work    returnFalse# Driver Programroot =Node(10);root.left =Node(20);root.right =Node(30);root.left.right =Node(40);root.left.left =Node(50);root.right.left =Node(60);root.right.right =Node(70);root.left.left.left =Node(80);root.left.left.right =Node(90);root.left.right.left =Node(80);root.left.right.right =Node(90);root.right.left.left =Node(80);root.right.left.right =Node(90);root.right.right.left =Node(80);root.right.right.right =Node(90);ifisFullTree(root):    print("The Binary tree is full")else:    print("Binary tree is not full")# This code is contributed by Nikhil Kumar Singh(nickzuck_007) | 
C#
| // C# program to check if binary tree // is full or not usingSystem;/* Tree node structure */publicclassNode{    publicintdata;    publicNode left, right;    publicNode(intitem)    {        data = item;        left = right = null;    }}classGFG{publicNode root;/* This function checks if a binarytree is full or not */publicvirtualboolisFullTree(Node node){    // if empty tree     if(node == null)    {    returntrue;    }    // if leaf node     if(node.left == null&& node.right == null)    {        returntrue;    }    // if both left and right subtrees     // are not null they are full     if((node.left != null) && (node.right != null))    {        return(isFullTree(node.left) &&                 isFullTree(node.right));    }    // if none work     returnfalse;}// Driver Code publicstaticvoidMain(string[] args){    GFG tree = newGFG();    tree.root = newNode(10);    tree.root.left = newNode(20);    tree.root.right = newNode(30);    tree.root.left.right = newNode(40);    tree.root.left.left = newNode(50);    tree.root.right.left = newNode(60);    tree.root.left.left.left = newNode(80);    tree.root.right.right = newNode(70);    tree.root.left.left.right = newNode(90);    tree.root.left.right.left = newNode(80);    tree.root.left.right.right = newNode(90);    tree.root.right.left.left = newNode(80);    tree.root.right.left.right = newNode(90);    tree.root.right.right.left = newNode(80);    tree.root.right.right.right = newNode(90);    if(tree.isFullTree(tree.root))    {        Console.Write("The binary tree is full");    }    else    {        Console.Write("The binary tree is not full");    }}}// This code is contributed by Shrikant13 | 
Javascript
| <script>// javascript program to check if binary tree is full or not/*  Tree node structure */class Node {    constructor(item) {        this.data = item;        this.left = this.right = null;    }}    varroot;    /* this function checks if a binary tree is full or not */    functionisFullTree( node) {        // if empty tree        if(node == null)            returntrue;        // if leaf node        if(node.left == null&& node.right == null)            returntrue;        // if both left and right subtrees are not null        // they are full        if((node.left != null) && (node.right != null))            return(isFullTree(node.left) && isFullTree(node.right));        // if none work        returnfalse;    }    // Driver program                root = newNode(10);        root.left = newNode(20);        root.right = newNode(30);        root.left.right = newNode(40);        root.left.left = newNode(50);        root.right.left = newNode(60);        root.left.left.left = newNode(80);        root.right.right = newNode(70);        root.left.left.right = newNode(90);        root.left.right.left = newNode(80);        root.left.right.right = newNode(90);        root.right.left.left = newNode(80);        root.right.left.right = newNode(90);        root.right.right.left = newNode(80);        root.right.right.right = newNode(90);         if(isFullTree(root))            document.write("The binary tree is full");        else            document.write("The binary tree is not full");            // This code contributed by gauravrajput1 </script> | 
The Binary Tree is full
Time complexity: O(n) where n is number of nodes in given binary tree.
Auxiliary Space: O(n) for call stack since using recursion
Iterative Approach:
To check whether a binary tree is a full binary tree we need to test the following cases:-
- Create a queue to store nodes
- Store the root of the tree in the queue
- Traverse until the queue is not empty
- If the current node is not a leaf insert root->left and root->right in the queue.
- If the current node is NULL return false.
 
- If the queue is empty return true.
Following is the implementation for checking if a binary tree is a full binary tree.
C++
| // c++ program to check whether a given BT is full or not#include <bits/stdc++.h>usingnamespacestd;// Tree node structurestructNode {    intval;    Node *left, *right;};// fun that creates and returns a new nodeNode* newNode(intdata){    Node* node = newNode();    node->val = data;    node->left = node->right = NULL;    returnnode;}// helper fun to check leafnodeboolisleafnode(Node* root){    return!root->left && !root->right;}// fun checks whether the given BT is a full BT or notboolisFullTree(Node* root){    // if tree is empty    if(!root)        returntrue;    queue<Node*> q;    q.push(root);    while(!q.empty()) {        root = q.front();        q.pop();        // null indicates - not a full BT        if(root == NULL)            returnfalse;        // if its not a leafnode then the current node        // should contain both left and right pointers.        if(!isleafnode(root)) {            q.push(root->left);            q.push(root->right);        }    }    returntrue;}intmain(){    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))        cout << "The Binary Tree is full\n";    else        cout << "The Binary Tree is not full\n";    return0;}// This code is contributed by Modem Upendra. | 
Java
| // Java program to check whether a given BT is full or notimportjava.util.ArrayDeque;importjava.util.Queue;publicclassGFG {  /*  Tree node structure */  staticclassNode {    intdata;    Node left, right;    Node(intitem)    {      data = item;      left = right = null;    }  }  // helper fun to check leafnode  staticbooleanisleafnode(Node root)  {    returnroot.left == null&& root.right == null;  }  // fun checks whether the given BT is a full BT or not  staticbooleanisFullTree(Node root)  {    // if tree is empty    if(root == null)      returntrue;    Queue<Node> q = newArrayDeque<>();    q.add(root);    while(!q.isEmpty()) {      root = q.peek();      q.remove();      // null indicates - not a full BT      if(root == null)        returnfalse;      // if its not a leafnode then the current node      // should contain both left and right pointers.      if(!isleafnode(root)) {        q.add(root.left);        q.add(root.right);      }    }    returntrue;  }  // Driver Code  publicstaticvoidmain(String[] args)  {    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))      System.out.println("The Binary Tree is full");    else      System.out.println(      "The Binary Tree is not full");  }}// This code is contributed by karandeep1234 | 
Python3
| # Python program to check whether a given BT is full or not# Tree StructureclassNode:    def__init__(self, key):        self.data =key        self.left =None        self.right =None# function that creates and returns a new nodedefnewNode(data):    node =Node(data)    returnnode# helper function to check leafnodedefisleafnode(root):    returnroot.left isnotNoneandroot.right isnotNone# function checks whether the given BT is a full BT or notdefisFullTree(root):    # if tree is empty    ifroot isNone:        returnTrue    q =[]    q.append(root)    while(len(q) > 0):        root =q.pop(0)        # null indicates - not a full BT        ifroot isNone:            returnFalse        # if its not a leafnode then the current node        # should contain both left and right pointers        ifisleafnode(root) isFalse:            q.append(root.left)            q.append(root.right)    returnTrue# Driver program to test above functionroot =newNode(1)root.left =newNode(2)root.right =newNode(3)root.left.left =newNode(4)root.left.right =newNode(5)ifisFullTree(root) isTrue:    print("The Binary Tree is full")else:    print("The Binary Tree is not full")# This code is contributed by Yash Agarwal(yashagarwal2852002) | 
C#
| // C# program to check whether a given BT is full or notusingSystem;usingSystem.Collections.Generic;publicclassGFG {    /*  Tree node structure */    publicclassNode {        publicintdata;        publicNode left, right;        publicNode(intitem)        {            data = item;            left = right = null;        }    }    // helper fun to check leafnode    staticboolisleafnode(Node root)    {        returnroot.left == null&& root.right == null;    }    // fun checks whether the given BT is a full BT or not    staticboolisFullTree(Node root)    {        // if tree is empty        if(root == null)            returntrue;        Queue<Node> q = newQueue<Node>();        q.Enqueue(root);        while(q.Count != 0) {            root = q.Dequeue();            // null indicates - not a full BT            if(root == null)                returnfalse;            // if its not a leafnode then the current node            // should contain both left and right pointers.            if(!isleafnode(root)) {                q.Enqueue(root.left);                q.Enqueue(root.right);            }        }        returntrue;    }    // Driver Code    publicstaticvoidMain(string[] args)    {        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))            Console.WriteLine("The Binary Tree is full");        else            Console.WriteLine(                "The Binary Tree is not full");    }}// This code is contributed by karandeep1234. | 
Javascript
| // JAVASCRIPT program to check whether a given BT is full or notclass Queue {    constructor() {        this.items = [];    }        // add element to the queue    enqueue(element) {        returnthis.items.push(element);    }        // remove element from the queue    dequeue() {        if(this.items.length > 0) {            returnthis.items.shift();        }    }        // view the last element    peek() {        returnthis.items[0];    }        // check if the queue is empty    isEmpty(){       returnthis.items.length == 0;    }       // the size of the queue    size(){        returnthis.items.length;    }     // empty the queue    clear(){        this.items = [];    }}// Tree node structureclass Node {    constructor(item) {        this.data = item;        this.left = this.right = null;    }}// helper fun to check leafnodefunctionisleafnode(root){    if(root.left==null&& root.right==null)        returntrue;    returnfalse;    }// fun checks whether the given BT is a full BT or notfunctionisFullTree( root){    // if tree is empty    if(root==null)        returntrue;  let q = newQueue();    q.enqueue(root)    while(q.size()!=0) {        root = q.peek();        q.dequeue();        // null indicates - not a full BT        if(root == null)            returnfalse;                // if its not a leafnode then the current node        // should contain both left and right pointers.        if(isleafnode(root)==false) {            q.enqueue(root.left);            q.enqueue(root.right);        }    }    returntrue;}    let 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)        console.log("The Binary Tree is full");    else        console.log("The Binary Tree is not full");   // This code is contributed by garg28harsh. | 
The Binary Tree is full
Time Complexity: O(N), Where N is the total nodes in a given binary tree.
Auxiliary Space: O(N), in most cases the last level contains nodes as half of the total nodes. O(N/2) ~ O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    








