GeeksforGeeks App
Open App
Browser
Continue

# Count of Nodes whose children gives same remainder when divided by K

Given a binary tree and an integer K. The task is to count the number of nodes having children that give the same remainder when divided by K. Print “-1” if no such node exists.

Examples:

Input:    2         K = 2
/ \
3   5
/    / \
7   8   6
Output: 2
Explanation: Children of 2 are 3 and 5. Both give remainder 1 with 2
Similarly for 5, both children give remainder as 0

Input: 9        K = 5
/ \
7   8
/ \
4   3
Output: -1
Explanation: There is no node having both children with same remainder with K.

Approach: This problem can be solved by simple binary tree traversal. Follow the steps below to solve the given problem.

• Traverse the Binary tree, and for each node, check
• If the node has a left child
• If the node has a right child
• If both children give the same remainder with K.
• Count all such nodes and print their content at the end.

Below is the implementation of the above approach.

## C++

 // C++ implementation to print// the nodes having a single child#include using namespace std; // Class of the Binary Tree nodestruct Node {    int data;    Node *left, *right;     Node(int x)    {        data = x;        left = right = NULL;    }}; // Function to find the nodes having both// and both of them % K are sameint countNodes(Node* root, int& K, int count){    // Base case    if (root == NULL)        return count;     // Condition to check if the    // node is having both child    // and both of them % K are same    if (root->left != NULL        && root->right != NULL        && root->left->data % K            == root->right->data % K) {         count++;    }     // Traversing the left child    count = countNodes(root->left, K, count);     // Traversing the right child    count = countNodes(root->right, K, count);    return count;} // Driver codeint main(){       // Constructing the binary tree    Node* root = new Node(2);    root->left = new Node(3);    root->right = new Node(5);    root->left->left = new Node(7);    root->right->left = new Node(8);    root->right->right = new Node(6);     int K = 2;     // Function calling    cout << countNodes(root, K, 0);}

## Java

 // Java code for the above approach import java.io.*;class Node {    int data;    Node left, right;     Node(int data)    {        this.data = data;        left = null;        right = null;    }}; class GFG {    // Function to find the nodes having both    // and both of them % K are same    static int countNodes(Node root, int K, int count)    {        // Base case        if (root == null)            return count;         // Condition to check if the        // node is having both child        // and both of them % K are same        if (root.left != null && root.right != null            && (root.left.data % K                == root.right.data % K)) {             count++;        }         // Traversing the left child        count = countNodes(root.left, K, count);         // Traversing the right child        count = countNodes(root.right, K, count);        return count;    }    public static void main(String[] args)    {        // Driver code         // Constructing the binary tree        Node root = new Node(2);        root.left = new Node(3);        root.right = new Node(5);        root.left.left = new Node(7);        root.right.left = new Node(8);        root.right.right = new Node(6);         int K = 2;         // Function calling         System.out.println(countNodes(root, K, 0));    }}//This code is contributed by Potta Lokesh

## Python3

 # Python code for the above approachclass Node:    def __init__(self, data):        self.data = data;        self.left = None;        self.right = None; # Function to find the Nodes having both# and both of them % K are samedef countNodes(root, K, count):       # Base case    if (root == None):        return count;     # Condition to check if the    # Node is having both child    # and both of them % K are same    if (root.left != None and root.right != None and (root.left.data % K == root.right.data % K)):         count += 1;         # Traversing the left child    count = countNodes(root.left, K, count);     # Traversing the right child    count = countNodes(root.right, K, count);    return count; if __name__ == '__main__':    # Driver code     # Constructing the binary tree    root =  Node(2);    root.left =  Node(3);    root.right =  Node(5);    root.left.left =  Node(7);    root.right.left =  Node(8);    root.right.right =  Node(6);     K = 2;     # Function calling    print(countNodes(root, K, 0)); # This code is contributed by umadevi9616

## C#

 // C# code for the above approachusing System;using System.Collections.Generic; class Node {  public int data;  public Node left, right;   public Node(int data)  {    this.data = data;    left = null;    right = null;  }}; public class GFG{   // Function to find the nodes having both  // and both of them % K are same  static int countNodes(Node root, int K, int count)  {     // Base case    if (root == null)      return count;     // Condition to check if the    // node is having both child    // and both of them % K are same    if (root.left != null && root.right != null        && (root.left.data % K            == root.right.data % K)) {       count++;    }     // Traversing the left child    count = countNodes(root.left, K, count);     // Traversing the right child    count = countNodes(root.right, K, count);    return count;  }  public static void Main(String[] args)  {    // Driver code     // Constructing the binary tree    Node root = new Node(2);    root.left = new Node(3);    root.right = new Node(5);    root.left.left = new Node(7);    root.right.left = new Node(8);    root.right.right = new Node(6);     int K = 2;     // Function calling     Console.WriteLine(countNodes(root, K, 0));  }} // This code is contributed by 29AjayKumar

## Javascript



Output

2

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up