Check whether a binary tree is a full binary tree or not | Iterative Approach

Given a binary tree containing n nodes. The problem is to check whether the given binary tree is a full binary tree or not. 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 only one child node.

Examples:

Input : 
           1
         /   \
        2     3
       / \  
      4   5
Output : Yes

Input :
           1
         /   \
        2     3
       /  
      4   
Output :No



Approach: In the previous post a recursive solution has been discussed. In this post an iterative approach has been followed. Perform iterative level order traversal of the tree using queue. For each node encountered, follow the steps given below:

  1. If (node->left == NULL && node->right == NULL), it is a leaf node. Discard it and start processing the next node from the queue.
  2. If (node->left == NULL || node->right == NULL), then it means that only child of node is present. Return false as the binary tree is not a full binary tree.
  3. Else, push the left and right child’s of the node on to the queue.

If all the node’s from the queue gets processed without returning false, then return true as the binary tree is a full binary tree.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to check whether a binary 
// tree is a full binary tree or not
#include <bits/stdc++.h>
using namespace std;
  
// structure of a node of binary tree
struct Node {
    int data;
    Node *left, *right;
};
  
// function to get a new node
Node* getNode(int data)
{
    // allocate space
    Node* newNode = (Node*)malloc(sizeof(Node));
  
    // put in the data
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
  
// function to check whether a binary tree
// is a full binary tree or not
bool isFullBinaryTree(Node* root)
{
    // if tree is empty
    if (!root)
        return true;
  
    // queue used for level oder traversal
    queue<Node*> q;
  
    // push 'root' to 'q'
    q.push(root);
  
    // traverse all the nodes of the binary tree
    // level by level until queue is empty
    while (!q.empty()) {
        // get the pointer to 'node' at front
        // of queue
        Node* node = q.front();
        q.pop();
  
        // if it is a leaf node then continue
        if (node->left == NULL && node->right == NULL)
            continue;
  
        // if either of the child is not null and the
        // other one is null, then binary tree is not
        // a full binary tee
        if (node->left == NULL || node->right == NULL)
            return false;
  
        // push left and right childs of 'node'
        // on to the queue 'q'
        q.push(node->left);
        q.push(node->right);
    }
  
    // binary tree is a full binary tee
    return true;
}
  
// Driver program to test above
int main()
{
    Node* root = getNode(1);
    root->left = getNode(2);
    root->right = getNode(3);
    root->left->left = getNode(4);
    root->left->right = getNode(5);
  
    if (isFullBinaryTree(root))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Output:

Yes

Time Complexity: O(n).
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.