Check whether a binary tree is a full binary tree or not | Iterative Approach
Last Updated :
28 Jul, 2022
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++
// 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 order 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;
}
Java
// Java implementation to check whether a binary
// tree is a full binary tree or not
import java.util.*;
class GfG {
// structure of a node of binary tree
static class Node {
int data;
Node left, right;
}
// function to get a new node
static Node getNode(int data)
{
// allocate space
Node newNode = new Node();
// put in the data
newNode.data = data;
newNode.left = null;
newNode.right = null;
return newNode;
}
// function to check whether a binary tree
// is a full binary tree or not
static boolean isFullBinaryTree(Node root)
{
// if tree is empty
if (root == null)
return true;
// queue used for level order traversal
Queue<Node> q = new LinkedList<Node> ();
// push 'root' to 'q'
q.add(root);
// traverse all the nodes of the binary tree
// level by level until queue is empty
while (!q.isEmpty()) {
// get the pointer to 'node' at front
// of queue
Node node = q.peek();
q.remove();
// 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.add(node.left);
q.add(node.right);
}
// binary tree is a full binary tee
return true;
}
// Driver program to test above
public static void main(String[] args)
{
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))
System.out.println("Yes");
else
System.out.println("No");
}
}
Python3
# Python3 program to find deepest
# left leaf Binary search Tree
# Helper function that allocates a
# new node with the given data and
# None left and right pairs.
class getNode:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# function to check whether a binary
# tree is a full binary tree or not
def isFullBinaryTree( root) :
# if tree is empty
if (not root) :
return True
# queue used for level order
# traversal
q = []
# append 'root' to 'q'
q.append(root)
# traverse all the nodes of the
# binary tree level by level
# until queue is empty
while (not len(q)):
# get the pointer to 'node'
# at front of queue
node = q[0]
q.pop(0)
# if it is a leaf node then continue
if (node.left == None and
node.right == None):
continue
# if either of the child is not None
# and the other one is None, then
# binary tree is not a full binary tee
if (node.left == None or
node.right == None):
return False
# append left and right childs
# of 'node' on to the queue 'q'
q.append(node.left)
q.append(node.right)
# binary tree is a full binary tee
return True
# Driver Code
if __name__ == '__main__':
root = getNode(1)
root.left = getNode(2)
root.right = getNode(3)
root.left.left = getNode(4)
root.left.right = getNode(5)
if (isFullBinaryTree(root)) :
print("Yes" )
else:
print("No")
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# implementation to check whether a binary
// tree is a full binary tree or not
using System;
using System.Collections.Generic;
class GfG
{
// structure of a node of binary tree
public class Node
{
public int data;
public Node left, right;
}
// function to get a new node
static Node getNode(int data)
{
// allocate space
Node newNode = new Node();
// put in the data
newNode.data = data;
newNode.left = null;
newNode.right = null;
return newNode;
}
// function to check whether a binary tree
// is a full binary tree or not
static bool isFullBinaryTree(Node root)
{
// if tree is empty
if (root == null)
return true;
// queue used for level order traversal
Queue<Node> q = new Queue<Node> ();
// push 'root' to 'q'
q.Enqueue(root);
// traverse all the nodes of the binary tree
// level by level until queue is empty
while (q.Count!=0) {
// get the pointer to 'node' at front
// of queue
Node node = q.Peek();
q.Dequeue();
// 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.Enqueue(node.left);
q.Enqueue(node.right);
}
// binary tree is a full binary tee
return true;
}
// Driver code
public static void Main(String[] args)
{
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))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// JavaScript implementation to check whether a binary
// tree is a full binary tree or not
// A Binary Tree Node
class Node
{
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
}
// function to get a new node
function getNode(data)
{
// allocate space
let newNode = new Node(data);
return newNode;
}
// function to check whether a binary tree
// is a full binary tree or not
function isFullBinaryTree(root)
{
// if tree is empty
if (root == null)
return true;
// queue used for level order traversal
let 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.length > 0) {
// get the pointer to 'node' at front
// of queue
let node = q[0];
q.shift();
// 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;
}
let root = getNode(1);
root.left = getNode(2);
root.right = getNode(3);
root.left.left = getNode(4);
root.left.right = getNode(5);
if (isFullBinaryTree(root))
document.write("Yes");
else
document.write("No");
</script>
Time Complexity: O(n).
Auxiliary Space: O(max), where max is the maximum number of nodes at a particular level.