Find nodes whose children have same modulo with K
Last Updated :
24 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node( int x)
{
data = x;
left = right = NULL;
}
};
vector< int > listOfNodes;
void countNodes(Node* root, int & K)
{
if (root == NULL)
return ;
if (root->left != NULL
&& root->right != NULL
&& root->left->data % K
== root->right->data % K) {
listOfNodes.push_back(root->data);
}
countNodes(root->left, K);
countNodes(root->right, K);
}
int main()
{
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;
countNodes(root, K);
if (listOfNodes.size() == 0)
printf ( "-1" );
else {
for ( int value : listOfNodes) {
cout << (value) << endl;
}
}
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node left, right;
Node( int x)
{
data = x;
left = right = null ;
}
};
static Vector<Integer> listOfNodes = new Vector<Integer>();
static void countNodes(Node root, int K)
{
if (root == null )
return ;
if (root.left != null
&& root.right != null
&& root.left.data % K
== root.right.data % K) {
listOfNodes.add(root.data);
}
countNodes(root.left, K);
countNodes(root.right, K);
}
public static void main(String[] args)
{
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 ;
countNodes(root, K);
if (listOfNodes.size() == 0 )
System.out.printf( "-1" );
else {
for ( int value : listOfNodes) {
System.out.print((value) + "\n" );
}
}
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = self .right = None
listOfNodes = []
def countNodes(root, K):
if (root = = None ):
return 0
if (root.left ! = None
and root.right ! = None
and root.left.data % K
= = root.right.data % K):
listOfNodes.append(root.data)
countNodes(root.left, K)
countNodes(root.right, K)
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
countNodes(root, K)
if ( len (listOfNodes) = = 0 ):
print ( "-1" )
else :
for value in listOfNodes:
print (value)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
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 >();
static void countNodes(Node root, int K)
{
if (root == null )
return ;
if (root.left != null
&& root.right != null
&& root.left.data % K
== root.right.data % K) {
listOfNodes.Add(root.data);
}
countNodes(root.left, K);
countNodes(root.right, K);
}
public static void Main(String[] args)
{
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;
countNodes(root, K);
if (listOfNodes.Count == 0)
Console.Write( "-1" );
else {
foreach ( int values in listOfNodes) {
Console.Write((values) + "\n" );
}
}
}
}
|
Javascript
<script>
class Node
{
constructor(x)
{
this .data = x;
this .left = this .right = null ;
}
};
let listOfNodes = [];
function countNodes(root, K)
{
if (root == null )
return 0;
if (root.left != null
&& root.right != null
&& root.left.data % K
== root.right.data % K) {
listOfNodes.push(root.data);
}
countNodes(root.left, K);
countNodes(root.right, K);
}
let 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);
let K = 2;
countNodes(root, K);
if (listOfNodes.length == 0)
document.write( "-1" );
else {
for (let value of listOfNodes) {
document.write((value) + "<br>" );
}
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...