📌  相关文章
📜  查找其子节点与 K 模数相同的节点

📅  最后修改于: 2022-05-13 01:56:07.933000             🧑  作者: Mango

查找其子节点与 K 模数相同的节点

给定一个二叉树和一个整数K ,任务是打印所有具有相同子节点的节点除以K时具有相同的余数。如果不存在这样的节点,则打印“ -1 ”。
例子

方法:可以使用深度优先搜索来解决该任务。遍历二叉树,对于每个节点,检查:

  • 如果节点有左孩子
  • 如果节点有右孩子
  • 如果两个孩子都给出与 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;
    }
};
 
vector 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 listOfNodes = new List();
 
  // 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



输出
2
5

时间复杂度:O(N)
辅助空间:O(1)