Check if the given n-ary tree is a binary tree
Given an n-ary tree, the task is to check whether the given tree is binary or not.
Examples:
Input:
A
/ \
B C
/ \ \
D E F
Output: Yes
Input:
A
/ | \
B C D
\
F
Output: No
Approach: Every node in a binary tree can have at most 2 children. So, for every node of the given tree, count the number of children and if for any node the count exceeds 2 then print No else print Yes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
char key;
vector<Node*> child;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
return temp;
}
bool isBinaryTree( struct Node* root)
{
if (!root)
return true ;
int count = 0;
for ( int i = 0; i < root->child.size(); i++) {
if (!isBinaryTree(root->child[i]))
return false ;
count++;
if (count > 2)
return false ;
}
return true ;
}
int main()
{
Node* root = newNode( 'A' );
(root->child).push_back(newNode( 'B' ));
(root->child).push_back(newNode( 'C' ));
(root->child[0]->child).push_back(newNode( 'D' ));
(root->child[0]->child).push_back(newNode( 'E' ));
(root->child[1]->child).push_back(newNode( 'F' ));
if (isBinaryTree(root))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int key;
Vector<Node> child = new Vector<Node>();
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
return temp;
}
static boolean isBinaryTree(Node root)
{
if (root == null )
return true ;
int count = 0 ;
for ( int i = 0 ; i < root.child.size(); i++)
{
if (!isBinaryTree(root.child.get(i)))
return false ;
count++;
if (count > 2 )
return false ;
}
return true ;
}
public static void main(String[] args)
{
Node root = newNode( 'A' );
(root.child).add(newNode( 'B' ));
(root.child).add(newNode( 'C' ));
(root.child.get( 0 ).child).add(newNode( 'D' ));
(root.child.get( 0 ).child).add(newNode( 'E' ));
(root.child.get( 1 ).child).add(newNode( 'F' ));
if (isBinaryTree(root))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
class Node:
def __init__( self ,key):
self .key = key
self .child = []
def newNode(key):
temp = Node(key)
return temp
def isBinaryTree(root):
if (root = = None ):
return True
count = 0
for i in range ( len (root.child)):
if (isBinaryTree(root.child[i]) = = False ):
return False
count + = 1
if (count > 2 ):
return False
return True
root = newNode( 'A' )
(root.child).append(newNode( 'B' ))
(root.child).append(newNode( 'C' ))
(root.child[ 0 ].child).append(newNode( 'D' ))
(root.child[ 0 ].child).append(newNode( 'E' ))
(root.child[ 1 ].child).append(newNode( 'F' ))
if (isBinaryTree(root)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int key;
public List<Node> child = new List<Node>();
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
return temp;
}
static bool isBinaryTree(Node root)
{
if (root == null )
return true ;
int count = 0;
for ( int i = 0; i < root.child.Count; i++)
{
if (!isBinaryTree(root.child[i]))
return false ;
count++;
if (count > 2)
return false ;
}
return true ;
}
public static void Main(String[] args)
{
Node root = newNode( 'A' );
(root.child).Add(newNode( 'B' ));
(root.child).Add(newNode( 'C' ));
(root.child[0].child).Add(newNode( 'D' ));
(root.child[0].child).Add(newNode( 'E' ));
(root.child[1].child).Add(newNode( 'F' ));
if (isBinaryTree(root))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .key = key;
this .child = [];
}
}
function newNode(key)
{
let temp = new Node(key);
return temp;
}
function isBinaryTree(root)
{
if (root == null )
return true ;
let count = 0;
for (let i = 0; i < root.child.length; i++)
{
if (!isBinaryTree(root.child[i]))
return false ;
count++;
if (count > 2)
return false ;
}
return true ;
}
let root = newNode('A ');
(root.child).push(newNode(' B '));
(root.child).push(newNode(' C '));
(root.child[0].child).push(newNode(' D '));
(root.child[0].child).push(newNode(' E '));
(root.child[1].child).push(newNode(' F'));
if (isBinaryTree(root))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Last Updated :
10 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...