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.
To check whether a binary tree is a full binary tree we need to test the following cases:- 1) If a binary tree node is NULL then it is a full binary tree. 2) If a binary tree node does have empty left and right sub-trees, then it is a full binary tree by definition. 3) 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. 4) 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++ program to check whether a given Binary Tree is full or not
/* Tree node structure */
structNode *left, *right;
/* Helper function that allocates a new node with the
given key and NULL left and right pointer. */
structNode *node = newNode;
node->key = k;
node->right = node->left = NULL;
/* This function tests if a binary tree is a full binary tree. */
boolisFullTree (structNode* root)
// If empty tree
if(root == NULL)
// If leaf node
if(root->left == NULL && root->right == NULL)
// If both left and right are not NULL, and left & right subtrees
Time complexity of the above code is O(n) where n is number of nodes in given binary tree.
This article is contributed by Gaurav Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.