📜  监控二叉树所有节点所需的最少摄像头数量

📅  最后修改于: 2021-09-17 07:42:29             🧑  作者: Mango

给定由N 个节点组成的二叉树,任务是找到监控整个树所需的最少摄像机数量,以便放置在任何节点的每个摄像机都可以监控节点本身、其父节点及其直接子节点。

例子:

方法:可以通过存储节点的状态来解决给定的问题,无论是否放置了摄像头,或者节点是否被任何其他具有摄像头的节点监控。这个想法是在给定的树上执行 DFS 遍历,并在每次递归调用中返回每个节点的状态。考虑以下转换作为函数返回的状态:

  • 如果值为1 ,则监视节点。
  • 如果值为2 ,则不监视节点。
  • 如果值为3 ,则该节点具有相机。

请按照以下步骤解决问题:

  • 初始化一个变量,比如count来存储监控给定树的所有节点所需的最小摄像机数量。
  • 创建一个函数,比如dfs(root) ,它获取给定树的根并返回每个节点的状态,无论是否放置了摄像头,或者该节点是否被任何其他拥有摄像头的节点监控,并执行以下步骤:
    • 如果节点的值为NULL ,则返回1,因为NULL节点始终受到监视。
    • 递归调用左子树和右子树,并将它们返回的值存储在变量LR 中
    • 如果LR 的值为1,则从当前递归调用返回2 ,因为当前根节点没有被监控。
    • 如果LR 的值为2则将count的值增加1作为左右节点之一不被监视并返回3
    • 否则,返回1
  • 从根调用上述递归函数,如果它返回的值为2 ,则将count的值增加1
  • 完成上述步骤后,将count的值打印为最终的摄像机数量。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Structure for a Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
 
    // Return the newly created node
    return (temp);
}
 
// Stores the minimum number of
// cameras required
int cnt = 0;
 
// Utility function to find minimum
// number of cameras required to
// monitor the entire tree
int minCameraSetupUtil(Node* root)
{
    // If root is NULL
    if (root == NULL)
        return 1;
 
    int L = minCameraSetupUtil(
        root->left);
    int R = minCameraSetupUtil(
        root->right);
 
    // Both the nodes are monitored
    if (L == 1 && R == 1)
        return 2;
 
    // If one of the left and the
    // right subtree is not monitored
    else if (L == 2 || R == 2) {
        cnt++;
        return 3;
    }
 
    // If the root node is monitored
    return 1;
}
 
// Function to find the minimum number
// of cameras required to monitor
// entire tree
void minCameraSetup(Node* root)
{
    int value = minCameraSetupUtil(root);
 
    // Print the count of cameras
    cout << cnt + (value == 2 ? 1 : 0);
}
 
// Driver Code
int main()
{
    // Given Binary Tree
    Node* root = newNode(0);
    root->left = newNode(0);
    root->left->left = newNode(0);
    root->left->left->left = newNode(0);
    root->left->left->left->right = newNode(0);
 
    minCameraSetup(root);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG {
    // TreeNode class
    static class Node {
        public int key;
        public Node left, right;
    };
 
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.key = key;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Stores the minimum number of
    // cameras required
    static int cnt = 0;
 
    // Utility function to find minimum
    // number of cameras required to
    // monitor the entire tree
    static int minCameraSetupUtil(Node root)
    {
        // If root is NULL
        if (root == null)
            return 1;
 
        int L = minCameraSetupUtil(root.left);
        int R = minCameraSetupUtil(root.right);
 
        // Both the nodes are monitored
        if (L == 1 && R == 1)
            return 2;
 
        // If one of the left and the
        // right subtree is not monitored
        else if (L == 2 || R == 2) {
            cnt++;
            return 3;
        }
 
        // If the root node is monitored
        return 1;
    }
 
    // Function to find the minimum number
    // of cameras required to monitor
    // entire tree
    static void minCameraSetup(Node root)
    {
        int value = minCameraSetupUtil(root);
 
        // Print the count of cameras
        System.out.println(cnt + (value == 2 ? 1 : 0));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Given Binary Tree
        Node root = newNode(0);
        root.left = newNode(0);
        root.left.left = newNode(0);
        root.left.left.left = newNode(0);
        root.left.left.left.right = newNode(0);
 
        minCameraSetup(root);
    }
}
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Structure for a Tree node
class Node:
     
    def __init__(self, k):
         
        self.key = k
        self.left = None
        self.right = None
 
# Stores the minimum number of
# cameras required
cnt = 0
 
# Utility function to find minimum
# number of cameras required to
# monitor the entire tree
def minCameraSetupUtil(root):
     
    global cnt
     
    # If root is None
    if (root == None):
        return 1
 
    L = minCameraSetupUtil(root.left)
    R = minCameraSetupUtil(root.right)
 
    # Both the nodes are monitored
    if (L == 1 and R == 1):
        return 2
 
    # If one of the left and the
    # right subtree is not monitored
    elif (L == 2 or R == 2):
        cnt += 1
        return 3
 
    # If the root node is monitored
    return 1
 
# Function to find the minimum number
# of cameras required to monitor
# entire tree
def minCameraSetup(root):
     
    value = minCameraSetupUtil(root)
 
    # Print the count of cameras
    print(cnt + (1 if value == 2 else 0))
 
# Driver Code
if __name__ == '__main__':
     
    # Given Binary Tree
    root = Node(0)
    root.left = Node(0)
    root.left.left = Node(0)
    root.left.left.left = Node(0)
    root.left.left.left.right = Node(0)
 
    minCameraSetup(root)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
   
    // TreeNode class
    class Node {
        public int key;
        public Node left, right;
    };
 
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.key = key;
        temp.left = temp.right = null;
        return temp;
    }
 
    // Stores the minimum number of
    // cameras required
    static int cnt = 0;
 
    // Utility function to find minimum
    // number of cameras required to
    // monitor the entire tree
    static int minCameraSetupUtil(Node root)
    {
       
        // If root is NULL
        if (root == null)
            return 1;
 
        int L = minCameraSetupUtil(root.left);
        int R = minCameraSetupUtil(root.right);
 
        // Both the nodes are monitored
        if (L == 1 && R == 1)
            return 2;
 
        // If one of the left and the
        // right subtree is not monitored
        else if (L == 2 || R == 2) {
            cnt++;
            return 3;
        }
 
        // If the root node is monitored
        return 1;
    }
 
    // Function to find the minimum number
    // of cameras required to monitor
    // entire tree
    static void minCameraSetup(Node root)
    {
        int value = minCameraSetupUtil(root);
 
        // Print the count of cameras
        Console.WriteLine(cnt + (value == 2 ? 1 : 0));
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Given Binary Tree
        Node root = newNode(0);
        root.left = newNode(0);
        root.left.left = newNode(0);
        root.left.left.left = newNode(0);
        root.left.left.left.right = newNode(0);
 
        minCameraSetup(root);
    }
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(H),其中 H 是给定树高度

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程