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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node( int x)
{
data = x;
left = right = NULL;
}
};
int countNodes(Node* root, int & K, int count)
{
if (root == NULL)
return count;
if (root->left != NULL
&& root->right != NULL
&& root->left->data % K
== root->right->data % K) {
count++;
}
count = countNodes(root->left, K, count);
count = countNodes(root->right, K, count);
return count;
}
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;
cout << countNodes(root, K, 0);
}
|
Java
import java.io.*;
class Node {
int data;
Node left, right;
Node( int data)
{
this .data = data;
left = null ;
right = null ;
}
};
class GFG {
static int countNodes(Node root, int K, int count)
{
if (root == null )
return count;
if (root.left != null && root.right != null
&& (root.left.data % K
== root.right.data % K)) {
count++;
}
count = countNodes(root.left, K, count);
count = countNodes(root.right, K, count);
return count;
}
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 ;
System.out.println(countNodes(root, K, 0 ));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self .left = None ;
self .right = None ;
def countNodes(root, K, count):
if (root = = None ):
return count;
if (root.left ! = None and root.right ! = None and (root.left.data % K = = root.right.data % K)):
count + = 1 ;
count = countNodes(root.left, K, count);
count = countNodes(root.right, K, count);
return count;
if __name__ = = '__main__' :
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 ;
print (countNodes(root, K, 0 ));
|
C#
using 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
{
static int countNodes(Node root, int K, int count)
{
if (root == null )
return count;
if (root.left != null && root.right != null
&& (root.left.data % K
== root.right.data % K)) {
count++;
}
count = countNodes(root.left, K, count);
count = countNodes(root.right, K, count);
return count;
}
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;
Console.WriteLine(countNodes(root, K, 0));
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
};
function countNodes(root, K, count)
{
if (root == null )
return count;
if (root.left != null && root.right != null
&& (root.left.data % K
== root.right.data % K)) {
count++;
}
count = countNodes(root.left, K, count);
count = countNodes(root.right, K, count);
return count;
}
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;
document.write(countNodes(root, K, 0));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...