Count nodes with two children at level L in a Binary Tree - GeeksforGeeks

06 Aug, 2021
Given a Binary tree, the task is to count the number of nodes with two children at a given level L.
Examples: 
Input: 
          1
         /  \
        2    3
       / \    \
      4   5    6
         /    / \
        7    8   9
L = 2
Output: 1

Input:
          20
         /   \
        8     22
       / \    / \
      5   3  4   25
     / \  / \     \
    1  10 2  14    6
L = 3
Output: 2
Approach: Initialize a variable count = 0. Recursively traverse the tree in a level order manner. If the current level is same as the given level, then check whether the current node has two children. If it has two children then increment the variable count.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to find number of full nodes// at a given level#include <bits/stdc++.h>using namespace std; // A binary tree nodestruct Node {    int data;    struct Node *left, *right;}; // Utility function to allocate memory for a new nodestruct Node* newNode(int data){    struct Node* node = new (struct Node);    node->data = data;    node->left = node->right = NULL;    return (node);} // Function that returns the height of binary treeint height(struct Node* root){    if (root == NULL)        return 0;     int lheight = height(root->left);    int rheight = height(root->right);     return max(lheight, rheight) + 1;} // Level Order traversal to find the number of nodes// having two childrenvoid LevelOrder(struct Node* root, int level, int& count){    if (root == NULL)        return;     if (level == 1 && root->left && root->right)        count++;     else if (level > 1) {        LevelOrder(root->left, level - 1, count);        LevelOrder(root->right, level - 1, count);    }} // Returns the number of full nodes// at a given levelint CountFullNodes(struct Node* root, int L){    // Stores height of tree    int h = height(root);     // Stores count of nodes at a given level    // that have two children    int count = 0;     LevelOrder(root, L, count);     return count;} // Driver codeint main(){    struct Node* root = newNode(7);    root->left = newNode(5);    root->right = newNode(6);    root->left->left = newNode(8);    root->left->right = newNode(1);    root->left->left->left = newNode(2);    root->left->left->right = newNode(11);    root->right->left = newNode(3);    root->right->right = newNode(9);    root->right->right->right = newNode(13);    root->right->right->left = newNode(10);    root->right->right->right->left = newNode(4);    root->right->right->right->right = newNode(12);     int L = 3;     cout << CountFullNodes(root, L);     return 0;}
// Java program to find number of full nodes// at a given levelclass GFG{ //INT classstatic class INT{    int a;} // A binary tree nodestatic class Node{    int data;    Node left, right;}; // Utility function to allocate memory for a new nodestatic Node newNode(int data){    Node node = new Node();    node.data = data;    node.left = node.right = null;    return (node);} // Function that returns the height of binary treestatic int height(Node root){    if (root == null)        return 0;     int lheight = height(root.left);    int rheight = height(root.right);     return Math.max(lheight, rheight) + 1;} // Level Order traversal to find the number of nodes// having two childrenstatic void LevelOrder( Node root, int level, INT count){    if (root == null)        return;     if (level == 1 && root.left!=null && root.right!=null)        count.a++;     else if (level > 1)    {        LevelOrder(root.left, level - 1, count);        LevelOrder(root.right, level - 1, count);    }} // Returns the number of full nodes// at a given levelstatic int CountFullNodes( Node root, int L){    // Stores height of tree    int h = height(root);     // Stores count of nodes at a given level    // that have two children    INT count =new INT();    count.a = 0;     LevelOrder(root, L, count);     return count.a;} // Driver codepublic static void main(String args[]){    Node root = newNode(7);    root.left = newNode(5);    root.right = newNode(6);    root.left.left = newNode(8);    root.left.right = newNode(1);    root.left.left.left = newNode(2);    root.left.left.right = newNode(11);    root.right.left = newNode(3);    root.right.right = newNode(9);    root.right.right.right = newNode(13);    root.right.right.left = newNode(10);    root.right.right.right.left = newNode(4);    root.right.right.right.right = newNode(12);     int L = 3;     System.out.print( CountFullNodes(root, L)); }} // This code is contributed by Arnab Kundu
# Python3 program to find number of# full nodes at a given level # INT classclass INT:      def __init__(self):                 self.a = 0 # A binary tree nodeclass Node:         def __init__(self, data):                 self.left = None        self.right = None        self.data = data  # Utility function to allocate# memory for a new nodedef newNode(data):     node = Node(data)         return node # Function that returns the# height of binary treedef height(root):     if (root == None):        return 0;      lheight = height(root.left);    rheight = height(root.right);      return max(lheight, rheight) + 1; # Level Order traversal to find the# number of nodes having two childrendef LevelOrder(root, level, count):     if (root == None):        return;      if (level == 1 and        root.left != None and       root.right != None):        count.a += 1      elif (level > 1):        LevelOrder(root.left,                   level - 1, count);        LevelOrder(root.right,                   level - 1, count);  # Returns the number of full nodes# at a given leveldef CountFullNodes(root, L):     # Stores height of tree    h = height(root);      # Stores count of nodes at a    # given level that have two children    count = INT()      LevelOrder(root, L, count);      return count.a # Driver code   if __name__=="__main__":         root = newNode(7);    root.left = newNode(5);    root.right = newNode(6);    root.left.left = newNode(8);    root.left.right = newNode(1);    root.left.left.left = newNode(2);    root.left.left.right = newNode(11);    root.right.left = newNode(3);    root.right.right = newNode(9);    root.right.right.right = newNode(13);    root.right.right.left = newNode(10);    root.right.right.right.left = newNode(4);    root.right.right.right.right = newNode(12);      L = 3;      print(CountFullNodes(root, L))     # This code is contributed by rutvik_56
// C# program to find number of full nodes// at a given levelusing System; class GFG{ // INT classpublic class INT{    public int a;} // A binary tree nodepublic class Node{    public int data;    public Node left, right;}; // Utility function to allocate memory for a new nodestatic Node newNode(int data){    Node node = new Node();    node.data = data;    node.left = node.right = null;    return (node);} // Function that returns the height of binary treestatic int height(Node root){    if (root == null)        return 0;     int lheight = height(root.left);    int rheight = height(root.right);     return Math.Max(lheight, rheight) + 1;} // Level Order traversal to find the number of nodes// having two childrenstatic void LevelOrder( Node root, int level, INT count){    if (root == null)        return;     if (level == 1 && root.left!=null && root.right!=null)        count.a++;     else if (level > 1)    {        LevelOrder(root.left, level - 1, count);        LevelOrder(root.right, level - 1, count);    }} // Returns the number of full nodes// at a given levelstatic int CountFullNodes( Node root, int L){    // Stores height of tree    int h = height(root);     // Stores count of nodes at a given level    // that have two children    INT count =new INT();    count.a = 0;     LevelOrder(root, L, count);     return count.a;} // Driver codepublic static void Main(String []args){    Node root = newNode(7);    root.left = newNode(5);    root.right = newNode(6);    root.left.left = newNode(8);    root.left.right = newNode(1);    root.left.left.left = newNode(2);    root.left.left.right = newNode(11);    root.right.left = newNode(3);    root.right.right = newNode(9);    root.right.right.right = newNode(13);    root.right.right.left = newNode(10);    root.right.right.right.left = newNode(4);    root.right.right.right.right = newNode(12);     int L = 3;     Console.Write( CountFullNodes(root, L)); }} // This code is contributed by 29AjayKumar
<script> // Javascript program to find number// of full nodes at a given level // INT classlet a = 0; // A binary tree nodeclass Node{    constructor(data)    {        this.left = null;        this.right = null;        this.data = data;    }} // Utility function to allocate memory// for a new nodefunction newNode(data){    let node = new Node(data);    return (node);} // Function that returns the height// of binary treefunction height(root){    if (root == null)        return 0;     let lheight = height(root.left);    let rheight = height(root.right);     return Math.max(lheight, rheight) + 1;} // Level Order traversal to find the number// of nodes having two childrenfunction LevelOrder(root, level){    if (root == null)        return;     if (level == 1 && root.left != null &&                     root.right != null)        a++;     else if (level > 1)    {        LevelOrder(root.left, level - 1);        LevelOrder(root.right, level - 1);    }} // Returns the number of full nodes// at a given levelfunction CountFullNodes(root, L){         // Stores height of tree    let h = height(root);     LevelOrder(root, L);     return a;} // Driver codelet root = newNode(7);root.left = newNode(5);root.right = newNode(6);root.left.left = newNode(8);root.left.right = newNode(1);root.left.left.left = newNode(2);root.left.left.right = newNode(11);root.right.left = newNode(3);root.right.right = newNode(9);root.right.right.right = newNode(13);root.right.right.left = newNode(10);root.right.right.right.left = newNode(4);root.right.right.right.right = newNode(12); let L = 3; document.write(CountFullNodes(root, L)); // This code is contributed by mukesh07 </script>
2
 
Time Complexity: O(N) Auxiliary Space: O(N)
andrew1234
29AjayKumar
rutvik_56
mukesh07
pankajsharmagfg
Binary Tree
Data Structures
Tree
Data Structures
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Tree Data Structure
Program to implement Singly Linked List in C++ using class
Hash Functions and list/types of Hash functions
Insertion in a B+ tree
Shortest path in a directed graph by Dijkstra’s algorithm
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
AVL Tree | Set 1 (Insertion)
Inorder Tree Traversal without Recursion