# Find nodes whose children have same modulo with K

• Last Updated : 22 Feb, 2022

Given a binary tree and an integer K, the task is to print all the nodes having children with the same remainder when divided by K. Print “-1” if no such node exists.
Examples:

Input: K = 2

2

/ \

3   5

/   / \

7   8   6

Output: 2, 5
Explanation: Children of 2 = 3 and 5. Both give remainder 1 with 2, Similarly for 5, both children give remainder as 0

Input: K = 5

9

/ \

7   8

/ \

4   3

Output: -1
Explanation: There is no node having both children with same remainder with K.

Approach: The task can be solved using a depth-first search. 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
• Store all such nodes in a vector, and print its 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 node` `struct` `Node {` `    ``int` `data;` `    ``Node *left, *right;`   `    ``Node(``int` `x)` `    ``{` `        ``data = x;` `        ``left = right = NULL;` `    ``}` `};`   `vector<``int``> listOfNodes;`   `// Function to find the nodes` `// having both child` `// and both of them % K are same` `void` `countNodes(Node* root, ``int``& K)` `{` `    ``// Base case` `    ``if` `(root == NULL)` `        ``return``;`   `    ``// 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) {`   `        ``listOfNodes.push_back(root->data);` `    ``}`   `    ``// Traversing the left child` `    ``countNodes(root->left, K);`   `    ``// Traversing the right child` `    ``countNodes(root->right, K);` `}`   `// Driver code` `int` `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` `    ``countNodes(root, K);`   `    ``// Condition to check if there is` `    ``// no such node having single child` `    ``if` `(listOfNodes.size() == 0)` `        ``printf``(``"-1"``);` `    ``else` `{` `        ``for` `(``int` `value : listOfNodes) {` `            ``cout << (value) << endl;` `        ``}` `    ``}` `}`

## Java

 `// Java implementation to print` `// the nodes having a single child` `import` `java.util.*;`   `class` `GFG{`   `// Class of the Binary Tree node` `static` `class` `Node {` `    ``int` `data;` `    ``Node left, right;`   `    ``Node(``int` `x)` `    ``{` `        ``data = x;` `        ``left = right = ``null``;` `    ``}` `};`   `static` `Vector listOfNodes = ``new` `Vector();`   `// Function to find the nodes` `// having both child` `// and both of them % K are same` `static` `void` `countNodes(Node root, ``int` `K)` `{` `    ``// Base case` `    ``if` `(root == ``null``)` `        ``return``;`   `    ``// 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) {`   `        ``listOfNodes.add(root.data);` `    ``}`   `    ``// Traversing the left child` `    ``countNodes(root.left, K);`   `    ``// Traversing the right child` `    ``countNodes(root.right, K);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``// 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` `    ``countNodes(root, K);`   `    ``// Condition to check if there is` `    ``// no such node having single child` `    ``if` `(listOfNodes.size() == ``0``)` `        ``System.out.printf(``"-1"``);` `    ``else` `{` `        ``for` `(``int` `value : listOfNodes) {` `            ``System.out.print((value) +``"\n"``);` `        ``}` `    ``}` `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python code for the above approach`   `# Class of the Binary Tree node` `class` `Node:` `    ``def` `__init__(``self``, x):` `        ``self``.data ``=` `x` `        ``self``.left ``=` `self``.right ``=` `None`   `listOfNodes ``=` `[]`   `# Function to find the nodes` `# having both child` `# and both of them % K are same` `def` `countNodes(root, K):`   `    ``# Base case` `    ``if` `(root ``=``=` `None``):` `        ``return` `0`   `    ``# 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):`   `        ``listOfNodes.append(root.data)`   `    ``# Traversing the left child` `    ``countNodes(root.left, K)`   `    ``# Traversing the right child` `    ``countNodes(root.right, K)`     `# 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` `countNodes(root, K)`   `# Condition to check if there is` `# no such node having single child` `if` `(``len``(listOfNodes) ``=``=` `0``):` `    ``print``(``"-1"``)` `else``:` `    ``for` `value ``in` `listOfNodes:` `        ``print``(value)`   `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# implementation to print` `// the nodes having a single child` `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG{`   `  ``// Class of the Binary Tree node` `  ``class` `Node {` `    ``public` `int` `data;` `    ``public` `Node left, right;`   `    ``public` `Node(``int` `x)` `    ``{` `      ``data = x;` `      ``left = right = ``null``;` `    ``}` `  ``};`   `  ``static` `List<``int``> listOfNodes = ``new` `List<``int``>();`   `  ``// Function to find the nodes` `  ``// having both child` `  ``// and both of them % K are same` `  ``static` `void` `countNodes(Node root, ``int` `K)` `  ``{` `    ``// Base case` `    ``if` `(root == ``null``)` `      ``return``;`   `    ``// 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) {`   `      ``listOfNodes.Add(root.data);` `    ``}`   `    ``// Traversing the left child` `    ``countNodes(root.left, K);`   `    ``// Traversing the right child` `    ``countNodes(root.right, K);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main(String[] args)` `  ``{` `    ``// 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` `    ``countNodes(root, K);`   `    ``// Condition to check if there is` `    ``// no such node having single child` `    ``if` `(listOfNodes.Count == 0)` `      ``Console.Write(``"-1"``);` `    ``else` `{` `      ``foreach` `(``int` `values ``in` `listOfNodes) {` `        ``Console.Write((values) +``"\n"``);` `      ``}` `    ``}` `  ``}` `}`   `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output

```2
5```

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :