📌  相关文章
📜  给定二叉树中左子树平均值至少为 K 的节点数

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

给定二叉树中左子树平均值至少为 K 的节点数

给定一棵二叉树和一个数字K,任务是计算其左子树中的平均值大于或等于K的节点的数量。

例子:

方法:按照以下步骤解决此问题:

  1. 创建一个全局变量ans来存储答案并用0对其进行初始化。
  2. 创建一个函数countHelper ,它将接受一个树节点和整数K作为参数,并将返回该节点的子树中的一对节点总和和节点数。
  3. 现在,在此函数的初始调用中传递根节点K。
  4. 在此递归函数的每次调用中:
    1. 检查当前节点是否为叶节点。如果它是叶节点,则只需返回{0, 0} ,因为该节点下方的节点的总和和数量都是0
    2. 现在,调用左右子树。
    3. 检查当前节点, (左右子树的总和/左右子树中的节点数)>=K ,如果它是按1递增
  5. 递归函数结束后,打印ans

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Structure of a tree node
class Node {
public:
    int data;
    Node *left, *right;
    Node(int d)
    {
        data = d;
        left = right = NULL;
    }
};
 
// Global variable to store the node count
int ans = 0;
 
// Function to count the nodes in a tree
// with average of all left nodes
// greater than or equal to K .
pair countHelper(Node* root,
                           int K)
{
 
    // For leaf Node
    if (!root->left and !root->right) {
        return { root->data, 1 };
    }
 
    pair left = { 0, 0 },
                   right = { 0, 0 };
 
    // For left subtree
    if (root->left) {
        left = countHelper(root->left, K);
    }
 
    // For right subtree
    if (root->right) {
        right = countHelper(root->right, K);
    }
    if (left.second != 0
        and left.first / left.second >= K) {
        ans += 1;
    }
 
    return { left.first + right.first + root->data,
             left.second + right.second + 1 };
}
 
// Function to call the initial
// instance of function countHelper
void countNodes(Node* root, int K)
{
    countHelper(root, K);
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given Tree
    Node* root = new Node(2);
    root->left = new Node(5);
    root->right = new Node(4);
    root->left->left = new Node(5);
    root->left->right = new Node(6);
    root->right->left = new Node(6);
    root->right->right = new Node(2);
    root->left->left->right = new Node(5);
    root->right->left->left = new Node(4);
 
    int K = 5;
 
    countNodes(root, K);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
 
static class pair
{
    int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
   
// Structure of a tree node
static class Node {
    int data;
    Node left, right;
    Node(int d)
    {
        data = d;
        left = right = null;
    }
};
 
// Global variable to store the node count
static int ans = 0;
 
// Function to count the nodes in a tree
// with average of all left nodes
// greater than or equal to K .
static pair countHelper(Node root,
                           int K)
{
 
    // For leaf Node
    if (root.left==null && root.right==null) {
        return new pair( root.data, 1 );
    }
 
    pair left = new pair( 0, 0 ),
                   right = new pair( 0, 0 );
 
    // For left subtree
    if (root.left!=null) {
        left = countHelper(root.left, K);
    }
 
    // For right subtree
    if (root.right!=null) {
        right = countHelper(root.right, K);
    }
    if (left.second != 0
        && left.first / left.second >= K) {
        ans += 1;
    }
 
    return new pair( left.first + right.first + root.data,
             left.second + right.second + 1 );
}
 
// Function to call the initial
// instance of function countHelper
static void countNodes(Node root, int K)
{
    countHelper(root, K);
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given Tree
    Node root = new Node(2);
    root.left = new Node(5);
    root.right = new Node(4);
    root.left.left = new Node(5);
    root.left.right = new Node(6);
    root.right.left = new Node(6);
    root.right.right = new Node(2);
    root.left.left.right = new Node(5);
    root.right.left.left = new Node(4);
 
    int K = 5;
 
    countNodes(root, K);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code for the above approach
class pair:
    def __init__(self, first, second):
        self.first = first;
        self.second = second;
 
# Structure of a tree node
class Node:
    def __init__(self, d):
        self.data = d;
        self.left = self.right = None;
 
# Global variable to store the node count
ans = 0;
 
# Function to count the nodes in a tree
# with average of all left nodes
# greater than or equal to K .
def countHelper(root, K):
 
    # For leaf Node
    if (root.left == None and root.right == None):
        return pair(root.data, 1);
 
    left = pair(0, 0)
    right = pair(0, 0)
 
    # For left subtree
    if (root.left != None):
        left = countHelper(root.left, K);
 
    # For right subtree
    if (root.right != None):
        right = countHelper(root.right, K);
     
    if (left.second != 0 and left.first / left.second >= K):
        global ans;
        ans += 1;
 
    return pair(left.first + right.first + root.data, left.second + right.second + 1);
 
# Function to call the initial
# instance of function countHelper
def countNodes(root, K):
    countHelper(root, K);
    print(ans);
 
# Driver Code
# Given Tree
root = Node(2);
root.left = Node(5);
root.right = Node(4);
root.left.left = Node(5);
root.left.right = Node(6);
root.right.left = Node(6);
root.right.right = Node(2);
root.left.left.right = Node(5);
root.right.left.left = Node(4);
 
K = 5;
 
countNodes(root, K);
 
# This code is contributed by Saurabh Jaiswal.


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  class pair
  {
    public int first, second;
    public pair(int first, int second) 
    {
      this.first = first;
      this.second = second;
    }   
  }
 
  // Structure of a tree node
  class Node {
    public int data;
    public Node left, right;
    public Node(int d)
    {
      data = d;
      left = right = null;
    }
  };
 
  // Global variable to store the node count
  static int ans = 0;
 
  // Function to count the nodes in a tree
  // with average of all left nodes
  // greater than or equal to K .
  static pair countHelper(Node root,
                          int K)
  {
 
    // For leaf Node
    if (root.left==null && root.right==null) {
      return new pair( root.data, 1 );
    }
 
    pair left = new pair( 0, 0 ),
    right = new pair( 0, 0 );
 
    // For left subtree
    if (root.left!=null) {
      left = countHelper(root.left, K);
    }
 
    // For right subtree
    if (root.right!=null) {
      right = countHelper(root.right, K);
    }
    if (left.second != 0
        && left.first / left.second >= K) {
      ans += 1;
    }
 
    return new pair( left.first + right.first + root.data,
                    left.second + right.second + 1 );
  }
 
  // Function to call the initial
  // instance of function countHelper
  static void countNodes(Node root, int K)
  {
    countHelper(root, K);
    Console.Write(ans);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Given Tree
    Node root = new Node(2);
    root.left = new Node(5);
    root.right = new Node(4);
    root.left.left = new Node(5);
    root.left.right = new Node(6);
    root.right.left = new Node(6);
    root.right.right = new Node(2);
    root.left.left.right = new Node(5);
    root.right.left.left = new Node(4);
 
    int K = 5;
 
    countNodes(root, K);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
3

时间复杂度: O(N) 其中 N 是树中的节点数
辅助空间: O(N)