📜  如果疫苗足以供近邻使用,则尽量减少为 N 栋房屋供应新冠疫苗

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

如果疫苗足以供近邻使用,则尽量减少为 N 栋房屋供应新冠疫苗

Geek 开发了一种有效的冠状病毒疫苗,他希望 Geek Land 的N栋房屋中的每一个都可以使用它。给定一棵二叉树,其中每个节点代表 Geek Land 中的一所房屋,如果一个疫苗套件足以满足该房屋、其父房屋及其直接子节点的需求,则找到应提供疫苗套件的最小房屋数量

例子:

方法:这个问题可以用动态规划来解决。

  • 创建一个哈希表以检查所有访问过的节点。
  • 递归函数应返回未访问的子节点的数量。
  • 如果节点为 NULL,则返回 0。这将是我们的基本条件。
  • 创建一个计数器变量来存储未访问的子节点的数量,并通过左右子节点的递归调用对其进行初始化。
    • 如果计数器变量为零,则所有子节点都已访问。如果当前节点未被访问且它不是根节点,则我们返回 1,因为存在一个节点,即当前节点未被访问。
    • 如果当前节点未被访问并且计数器变量也为零,并且如果当前节点是根节点,那么我们将答案增加 1 并返回 1。
    • 否则,如果计数器变量大于零,我们将父节点、当前节点和子节点标记为已访问,并将答案加 1。

下面是上述方法的实现。

C++
// C++ code to implement the approach
#include 
using namespace std;
 
// Structure of a tree node
struct Node {
    int data;
    Node* left;
    Node* right;
 
    Node(int val)
    {
        data = val;
        left = right = NULL;
    }
};
 
// Function to implement the dp
int solve(int& ans, Node* root, Node* parent,
          unordered_map& dp)
{
    // Base Condition
    if (root == 0)
        return 0;
 
    // Counter Variable to store the number
    // of unvisited child elements for
    // the current node
    int cnt = solve(ans, root->left, root, dp);
    cnt += solve(ans, root->right, root, dp);
 
    // If there are no unvisited child nodes
    if (cnt == 0) {
         
        // If the current node is root
        // node and unvisited increment
        // the answer by 1
        if (dp[root] == 0 and parent == 0) {
            ans++;
            return 1;
        }
        // If the current node is unvisited
        // but it is not the root node
        if (dp[root] == 0)
            return 1;
 
        // If the current node is also visited
        return 0;
    }
 
    // If there are unvisited child nodes
    else {
        // Mark the current node,
        // parent node and child
        // nodes as visited
        if (root->left != 0)
            dp[root->left] = 1;
        if (root->right != 0)
            dp[root->right] = 1;
        dp[root] = 1;
        if (parent != 0)
            dp[parent] = 1;
 
        // Increment the answer by 1
        ans++;
 
        // Return 0 as now we have marked
        // all nodes as visited
        return 0;
    }
}
 
// Function to find required vaccines
int supplyVaccine(Node* root)
{
    unordered_map dp;
    int ans = 0;
 
    // Passing parent of root
    // node as NULL to identify it
    solve(ans, root, 0, dp);
    return ans;
}
 
// Driver Code
int main()
{
    string treeString;
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->right->right = new Node(4);
    root->right->right->right = new Node(5);
    root->right->right->right->right
        = new Node(6);
     
    // Function call
    cout << supplyVaccine(root) << "\n";
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
class GFG {
 
  // Structure of a tree node
  static class Node {
    int data;
    Node left;
    Node right;
 
    Node(int val) {
      data = val;
      left = right = null;
    }
  };
 
  static int ans;
  static HashMap dp;
 
  // Function to implement the dp
  static int solve(Node root, Node parent)
  {
 
    // Base Condition
    if (root == null)
      return 0;
 
    // Counter Variable to store the number
    // of unvisited child elements for
    // the current node
    ans = solve(root.left, root);
    ans += solve(root.right, root);
 
    // If there are no unvisited child nodes
    if (ans == 0) {
 
      // If the current node is root
      // node and unvisited increment
      // the answer by 1
      if (dp.get(root) == null && parent == null) {
        ans++;
        return 1;
      }
      // If the current node is unvisited
      // but it is not the root node
      if (dp.get(root) == null)
        return 1;
 
      // If the current node is also visited
 
    }
 
    // If there are unvisited child nodes
    else
    {
 
      // Mark the current node,
      // parent node and child
      // nodes as visited
      if (root.left != null)
        dp.put(root.left, 1);
      if (root.right != null)
        dp.put(root.right, 1);
      dp.put(root, 1);
      if (parent != null)
        dp.put(parent, 1);
 
      // Return 0 as now we have marked
      // all nodes as visited
    }
    return 0;
  }
 
  // Function to find required vaccines
  static void supplyVaccine(Node root) {
    dp = new HashMap<>();
    ans = 0;
 
    // Passing parent of root
    // node as null to identify it
    solve(root, root);
 
  }
 
  // Driver Code
  public static void main(String[] args) {
    String treeString;
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.right.right = new Node(4);
    root.right.right.right = new Node(5);
    root.right.right.right.right = new Node(6);
 
    // Function call
    supplyVaccine(root);
    System.out.print(ans + "\n");
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code to implement the approach
 
# Structure of a tree node
class Node:
 
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None
 
dp = {}
ans = 0
 
# Function to implement the dp
def solve(root,  parent):
 
    global dp
    global ans
 
    # Base Condition
    if (root == None):
        return 0
 
    # Counter Variable to store the number
    # of unvisited child elements for
    # the current node
    ans = solve(root.left, root)
    ans += solve(root.right, root)
 
    # If there are no unvisited child nodes
    if (ans == 0):
 
        # If the current node is root
        # node and unvisited increment
        # the answer by 1
        if (root not in dp and parent == None):
            ans += 1
            return 1
        # If the current node is unvisited
        # but it is not the root node
        if (root not in dp):
            return 1
 
        # If the current node is also visited
 
 
    # If there are unvisited child nodes
    else:
 
        # Mark the current node,
        # parent node and child
        # nodes as visited
        if (root.left != None):
            dp[root.left] = 1
        if (root.right != None):
            dp[root.right] = 1
            dp[root] = 1
        if (parent != None):
            dp[parent] = 1
 
    # Return 0 as now we have marked
    # all nodes as visited
    return 0
 
# Function to find required vaccines
def supplyVaccine(root):
     
    global ans
    # Passing parent of root
    # node as None to identify it
    solve(root, root)
 
# Driver Code
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.right.right = Node(4)
root.right.right.right = Node(5)
root.right.right.right.right = Node(6)
 
# Function call
supplyVaccine(root)
print(ans)
 
# This code is contributed by shinjanpatra


Javascript



输出
2

时间复杂度: 在)。
辅助空间: 在)