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:
- If (node->left == NULL && node->right == NULL), it is a leaf node. Discard it and start processing the next node from the queue.
- 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.
- 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.
// 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; } |
Output:
Yes
Time Complexity: O(n).
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level.
Recommended Posts:
- Check for Symmetric Binary Tree (Iterative Approach)
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Deepest right leaf node in a binary tree | Iterative approach
- Count full nodes in a Binary tree (Iterative and Recursive)
- Deepest left leaf node in a binary tree | iterative approach
- Check whether a binary tree is a full binary tree or not
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a given binary tree is skewed binary tree or not?
- Print all full nodes in a Binary Tree
- Iterative Search for a key 'x' in Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
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.



