当除以 K 时,其子节点给出相同余数的节点数
给定一棵二叉树和一个整数K 。任务是计算具有子节点的节点数,这些节点在除以K时给出相同的余数。如果不存在这样的节点,则打印“-1” 。
例子:
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.
方法:这个问题可以通过简单的二叉树遍历来解决。请按照以下步骤解决给定的问题。
- 遍历二叉树,对每个节点,检查
- 如果节点有左孩子
- 如果节点有右孩子
- 如果两个孩子都给K相同的余数。
- 计算所有这样的节点并在最后打印它们的内容。
下面是上述方法的实现。
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;
}
};
// Function to find the nodes having both
// and both of them % K are same
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;
}
// 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
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 approach
class 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 same
def 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 approach
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
{
// 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
输出
2
时间复杂度: O(N)
辅助空间: O(1)