# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Structure of a node ` `// of an n-ary tree ` `struct` `Node { ` `    ``char` `key; ` `    ``vector child; ` `}; ` ` `  `// Utility function to create ` `// a new tree node ` `Node* newNode(``int` `key) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->key = key; ` `    ``return` `temp; ` `} ` ` `  `// Function that returns true ` `// if the given tree is binary ` `bool` `isBinaryTree(``struct` `Node* root) ` `{ ` ` `  `    ``// Base case ` `    ``if` `(!root) ` `        ``return` `true``; ` ` `  `    ``// Count will store the number of ` `    ``// children of the current node ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 0; i < root->child.size(); i++) { ` ` `  `        ``// If any child of the current node doesn't ` `        ``// satify the condition of being ` `        ``// a binary tree node ` `        ``if` `(!isBinaryTree(root->child[i])) ` `            ``return` `false``; ` ` `  `        ``// Increment the count of children ` `        ``count++; ` ` `  `        ``// If current node has more ` `        ``// than 2 children ` `        ``if` `(count > 2) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``Node* root = newNode(``'A'``); ` `    ``(root->child).push_back(newNode(``'B'``)); ` `    ``(root->child).push_back(newNode(``'C'``)); ` `    ``(root->child->child).push_back(newNode(``'D'``)); ` `    ``(root->child->child).push_back(newNode(``'E'``)); ` `    ``(root->child->child).push_back(newNode(``'F'``)); ` ` `  `    ``if` `(isBinaryTree(root)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` `class` `GFG  ` `{ ` ` `  `// Structure of a node ` `// of an n-ary tree ` `static` `class` `Node  ` `{ ` `    ``int` `key; ` `    ``Vector child = ``new` `Vector(); ` `}; ` ` `  `// Utility function to create ` `// a new tree node ` `static` `Node newNode(``int` `key) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.key = key; ` `    ``return` `temp; ` `} ` ` `  `// Function that returns true ` `// if the given tree is binary ` `static` `boolean` `isBinaryTree(Node root) ` `{ ` ` `  `    ``// Base case ` `    ``if` `(root == ``null``) ` `        ``return` `true``; ` ` `  `    ``// Count will store the number of ` `    ``// children of the current node ` `    ``int` `count = ``0``; ` `    ``for` `(``int` `i = ``0``; i < root.child.size(); i++)  ` `    ``{ ` ` `  `        ``// If any child of the current node doesn't ` `        ``// satify the condition of being ` `        ``// a binary tree node ` `        ``if` `(!isBinaryTree(root.child.get(i))) ` `            ``return` `false``; ` ` `  `        ``// Increment the count of children ` `        ``count++; ` ` `  `        ``// If current node has more ` `        ``// than 2 children ` `        ``if` `(count > ``2``) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `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"``); ` `} ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG  ` `{ ` ` `  `// Structure of a node ` `// of an n-ary tree ` `public` `class` `Node  ` `{ ` `    ``public` `int` `key; ` `    ``public` `List child = ``new` `List(); ` `}; ` ` `  `// Utility function to create ` `// a new tree node ` `static` `Node newNode(``int` `key) ` `{ ` `    ``Node temp = ``new` `Node(); ` `    ``temp.key = key; ` `    ``return` `temp; ` `} ` ` `  `// Function that returns true ` `// if the given tree is binary ` `static` `bool` `isBinaryTree(Node root) ` `{ ` ` `  `    ``// Base case ` `    ``if` `(root == ``null``) ` `        ``return` `true``; ` ` `  `    ``// Count will store the number of ` `    ``// children of the current node ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 0; i < root.child.Count; i++)  ` `    ``{ ` ` `  `        ``// If any child of the current node doesn't ` `        ``// satify the condition of being ` `        ``// a binary tree node ` `        ``if` `(!isBinaryTree(root.child[i])) ` `            ``return` `false``; ` ` `  `        ``// Increment the count of children ` `        ``count++; ` ` `  `        ``// If current node has more ` `        ``// than 2 children ` `        ``if` `(count > 2) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``Node root = newNode(``'A'``); ` `    ``(root.child).Add(newNode(``'B'``)); ` `    ``(root.child).Add(newNode(``'C'``)); ` `    ``(root.child.child).Add(newNode(``'D'``)); ` `    ``(root.child.child).Add(newNode(``'E'``)); ` `    ``(root.child.child).Add(newNode(``'F'``)); ` ` `  `    ``if` `(isBinaryTree(root)) ` `        ``Console.WriteLine(``"Yes"``); ` `    ``else` `        ``Console.WriteLine(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```Yes
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Improved By : princiraj1992, 29AjayKumar

Article Tags :
Practice Tags :

2

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