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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
bool isFullBinaryTree(Node* root)
{
if (!root)
return true ;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* node = q.front();
q.pop();
if (node->left == NULL && node->right == NULL)
continue ;
if (node->left == NULL || node->right == NULL)
return false ;
q.push(node->left);
q.push(node->right);
}
return true ;
}
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
import java.util.*;
class GfG {
static class Node {
int data;
Node left, right;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left = null ;
newNode.right = null ;
return newNode;
}
static boolean isFullBinaryTree(Node root)
{
if (root == null )
return true ;
Queue<Node> q = new LinkedList<Node> ();
q.add(root);
while (!q.isEmpty()) {
Node node = q.peek();
q.remove();
if (node.left == null && node.right == null )
continue ;
if (node.left == null || node.right == null )
return false ;
q.add(node.left);
q.add(node.right);
}
return true ;
}
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
class getNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def isFullBinaryTree( root) :
if ( not root) :
return True
q = []
q.append(root)
while ( not len (q)):
node = q[ 0 ]
q.pop( 0 )
if (node.left = = None and
node.right = = None ):
continue
if (node.left = = None or
node.right = = None ):
return False
q.append(node.left)
q.append(node.right)
return True
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" )
|
C#
using System;
using System.Collections.Generic;
class GfG
{
public class Node
{
public int data;
public Node left, right;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.left = null ;
newNode.right = null ;
return newNode;
}
static bool isFullBinaryTree(Node root)
{
if (root == null )
return true ;
Queue<Node> q = new Queue<Node> ();
q.Enqueue(root);
while (q.Count!=0) {
Node node = q.Peek();
q.Dequeue();
if (node.left == null && node.right == null )
continue ;
if (node.left == null || node.right == null )
return false ;
q.Enqueue(node.left);
q.Enqueue(node.right);
}
return true ;
}
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" );
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function getNode(data)
{
let newNode = new Node(data);
return newNode;
}
function isFullBinaryTree(root)
{
if (root == null )
return true ;
let q = [];
q.push(root);
while (q.length > 0) {
let node = q[0];
q.shift();
if (node.left == null && node.right == null )
continue ;
if (node.left == null || node.right == null )
return false ;
q.push(node.left);
q.push(node.right);
}
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.
Please Login to comment...