📜  给定二叉树中 K 的计数频率

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

给定二叉树中 K 的计数频率

给定N个节点的二叉树。计算整数K在二叉树中出现的频率。

例子:

方法:问题的解决方案是基于给定二叉树的遍历。请按照以下步骤操作:

  • 执行给定二叉树的中序遍历
  • 对于树中的每个节点,检查它是否等于K
  • 如果它等于K ,则将所需的计数增加1
  • 最后,返回最终计数。

下面是上述方法的实现。

C++
// C++ code to implement the approach
#include 
using namespace std;
 
// Structure of a tree node
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
    Node(int data)
    {
        this->data = data;
        left = right = NULL;
    }
};
 
// Function for inorder tree traversal
int countOccurrence(struct Node* root, int K)
{
    stack s;
    Node* curr = root;
 
    // Variable for counting frequency of K
    int count = 0;
 
    while (curr != NULL || s.empty() == false) {
 
        // Reach the left most Node
        // of the curr Node
        while (curr != NULL) {
 
            // Place pointer to a tree node
            // on the stack before
            // traversing the node's
            // left subtree
            s.push(curr);
            curr = curr->left;
        }
 
        // Current must be NULL at this point
        curr = s.top();
        s.pop();
 
        // Increment count if element = K
        if (curr->data == K)
            count++;
 
        // Traverse the right subtree
        curr = curr->right;
    }
 
    return count;
}
 
// Driver code
int main()
{
 
    // Binary tree as shown in example
    struct Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(2);
    root->left->left = new Node(4);
    root->left->right = new Node(2);
 
    int K = 2;
 
    // Function call
    int ans = countOccurrence(root, K);
    cout << ans << endl;
    return 0;
}


Java
// JAVA code to implement the approach
import java.util.*;
 
// Structure of a tree node
class Node {
    int data;
    Node left;
    Node right;
    Node(int data)
    {
        this.data = data;
        left = right = null;
    }
}
class GFG {
    // Function for inorder tree traversal
    public static int countOccurrence(Node root, int K)
    {
        Stack s = new Stack();
        Node curr = root;
 
        // Variable for counting frequency of K
        int count = 0;
 
        while (curr != null || s.empty() == false) {
 
            // Reach the left most Node
            // of the curr Node
            while (curr != null) {
 
                // Place pointer to a tree node
                // on the stack before
                // traversing the node's
                // left subtree
                s.push(curr);
                curr = curr.left;
            }
 
            // Current must be NULL at this point
            curr = s.peek();
            s.pop();
 
            // Increment count if element = K
            if (curr.data == K)
                count++;
 
            // Traverse the right subtree
            curr = curr.right;
        }
 
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Binary tree as shown in example
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(2);
        root.left.left = new Node(4);
        root.left.right = new Node(2);
 
        int K = 2;
 
        // Function call
        int ans = countOccurrence(root, K);
        System.out.println(ans);
    }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
 
# Structure of a tree node
class Node:
    def __init__(self,d):
        self.data = d
        self.left = None
        self.right = None
 
# Function for inorder tree traversal
def countOccurrence(root, K):
    s = []
    curr = root
 
    # Variable for counting frequency of K
    count = 0
 
    while (curr != None or len(s) != 0):
 
        # Reach the left most Node
        # of the curr Node
        while (curr != None):
 
            # Place pointer to a tree node
            # on the stack before
            # traversing the node's
            # left subtree
            s.append(curr)
            curr = curr.left
 
        # Current must be None at this point
        curr = s[len(s) - 1]
        s.pop()
 
        # Increment count if element = K
        if (curr.data == K):
            count += 1
 
        # Traverse the right subtree
        curr = curr.right
 
    return count
 
# Driver code
 
# Binary tree as shown in example
root = Node(1)
root.left = Node(2)
root.right = Node(2)
root.left.left = Node(4)
root.left.right = Node(2)
 
K = 2
 
# Function call
ans = countOccurrence(root, K)
print(ans)
 
 # This code is contributed by shinjanpatra


Javascript



输出
3

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