統計給定樹中權重是 X 的倍數的節點數


任務是檢查給定樹中樞節點的數量,其中每個樞節點的權重都可以被給定數字 X 整除。為了實現這一點,我們以精確的方式遍歷樹,分析每個樞節點及其權重。如果樞節點的權重可以被 X 整除,則我們增加一個計數器。我們對樹中的所有樞節點重複此過程。最後,計數器的值表示樹中權重為 X 的倍數的樞節點總數。這種方法確保我們僅識別和統計滿足所需條件的樞節點,從而得到準確的數量。

使用的方法

  • 遞迴深度優先搜尋 (DFS)

  • 中序遍歷

遞迴深度優先搜尋

遞迴深度優先搜尋 (DFS) 是一種以有序方式遍歷樹的策略,同時檢查權重可以被 X 整除的樞節點。從根節點開始,我們檢查當前樞節點的權重是否可以被 X 整除。如果是,則我們增加計數器。然後,我們遞迴地對左子樹和右子樹應用相同的處理。這種方法確保每個節點都被訪問,並且當節點滿足給定條件時,計數器會相應地增加。透過以深度優先的方式遞迴遍歷樹,我們可以有效地統計滿足權重計算條件的節點。

演算法

  • 從一個名為 countNodes(root, X) 的函式開始,該函式接受樹的根節點和除數 X 作為引數。

  • 將一個計數器變數初始化為 0。

  • 檢查當前節點 (root) 的權重是否可以被 X 整除。如果是,則增加計數器。

  • 遞迴呼叫 countNodes 函式,傳遞當前節點的左子節點和 X 作為引數。

  • 遞迴呼叫 countNodes 函式,傳遞當前節點的右子節點和 X 作為引數。

  • 返回計數器的最終值。

  • 使用樹的根節點和指定的除數 X 呼叫 countNodes 函式,以獲取權重可以被 X 整除的節點總數。

示例

#include <iostream>

struct Node {
   int weight;
   Node* left;
   Node* right;

   Node(int val) : weight(val), left(nullptr), right(nullptr) {}
};

int countNodes(Node* root, int X) {
   if (root == nullptr) {
      return 0;
   }

   int count = 0;

   if (root->weight % X == 0) {
      count++;
   }

   count += countNodes(root->left, X);
   count += countNodes(root->right, X);

   return count;
}

int main() {
   // Example usage

   // Constructing the tree
   Node* root = new Node(10);
   root->left = new Node(15);
   root->right = new Node(20);
   root->left->left = new Node(30);
   root->left->right = new Node(35);
   root->right->left = new Node(40);
   root->right->right = new Node(45);

   // Counting nodes with weight divisible by 5
   int divisor = 5;
   int result = countNodes(root, divisor);

   std::cout << "Number of nodes with weight divisible by " << divisor << ": " << result << std::endl;

   // Clean up the dynamically allocated memory (deallocate the tree)
   delete root->left->left;
   delete root->left->right;
   delete root->right->left;
   delete root->right->right;
   delete root->left;
   delete root->right;
   delete root;

   return 0;
}

輸出

Number of nodes with weight divisible by 5: 7

中序遍歷

在統計樹中權重可以被 X 整除的節點的上下文中,中序遍歷是一種以特定順序訪問樹節點的策略。從根節點開始,我們遞迴遍歷左子樹;然後,我們訪問當前節點,最後,我們遍歷右子樹。在此遍歷期間,在每個節點處,我們檢查權重是否可以被 X 整除。如果是,則我們增加計數器。透過這種方法,我們系統地檢視樹中的每個節點,並統計滿足權重可以被 X 整除條件的節點。

演算法

  • 將一個計數器變數初始化為 0。

  • 從樹的根節點開始。

  • 如果當前節點無效,則返回。

  • 遞迴遍歷左子樹。

  • 在當前節點處,檢查權重是否可以被 X 整除。

  • 如果是,則增加計數器。

  • 遞迴遍歷右子樹。

  • 對樹中的每個節點重複步驟 2-6。

  • 一旦所有節點都被訪問,計數器的值表示樹中權重可以被 X 整除的節點的總數。

  • 返回計數器的值。

示例

#include <iostream>

// Structure for a tree node
struct Node {
   int weight;
   Node* left;
   Node* right;
};

// Function to perform in-order traversal and count nodes divisible by X
int countNodesDivisibleByX(Node* root, int X) {
   if (root == nullptr) {
      return 0;
   }

   int counter = 0;

   // In-order traversal
   counter += countNodesDivisibleByX(root->left, X);

   // Check if current node's weight is divisible by X
   if (root->weight % X == 0) {
      counter++;
   }

   counter += countNodesDivisibleByX(root->right, X);

   return counter;
}

int main() {
   // Create a sample tree
   Node* root = new Node();
   root->weight = 10;

   Node* node1 = new Node();
   node1->weight = 20;

   Node* node2 = new Node();
   node2->weight = 15;

   Node* node3 = new Node();
   node3->weight = 30;

   root->left = node1;
   root->right = node2;
   node1->left = node3;
   node1->right = nullptr;
   node2->left = nullptr;
   node2->right = nullptr;
   node3->left = nullptr;
   node3->right = nullptr;

   // Specify the value of X
   int X = 5;

   // Count nodes divisible by X
   int count = countNodesDivisibleByX(root, X);

   // Output the result
   std::cout << "Number of nodes divisible by " << X << ": " << count << std::endl;

   // Clean up memory
   delete node3;
   delete node2;
   delete node1;
   delete root;

   return 0;
}

輸出

Number of nodes divisible by 5: 4

結論

本文討論了檢查給定樹中權重可以被給定數字 X 整除的節點的問題。它提出了多種方法,包括遞迴深度優先搜尋 (DFS) 和中序遍歷,以解決該問題。本文為每種方法提供了演算法步驟,並概述了使用 C 程式碼示例的實現。它強調了有序樹遍歷和條件檢查對於準確統計滿足所需權重可除性條件的節點的重要性。目標是全面瞭解該問題,並提供不同的方法來有效地解決它。

更新於: 2023年7月19日

70 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.