LeetCode 968.BinaryTreeCameras monitors binary trees

Topic link

LC.968

answer

Title

emm is like the title~

Ideas

We define "coverage" as the node that can be monitored.
Suppose the node has three states:

  1. Not covered
  2. Have a camera
  3. Has been overwritten.

In order to use the least number of cameras and monitor as many nodes as possible, we have to install cameras from the bottom up.
In order to complete this operation, we need to use post-order traversal.

Regarding the returned state, we start from the child node of the leaf node-the empty node.
For an empty node:

  1. If its status is not covered, then a camera needs to be installed at its parent node, causing waste;
  2. If its status is a camera, the status of its parent node is covered, causing the camera to be installed at the parent node of its parent node's parent node (grandpa node~), causing its parent node—leaf node to fail to be cover;
  3. If its status is covered, the status of its parent node is not covered, so that the camera is installed at the parent node of its parent node, perfect~

Finally, the root node needs to be processed. If the return status is not covered, the final result needs to be increased by 1.

AC code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    int ans = 0;

    int dfs(TreeNode* root) {
    
    
        if (root == nullptr) {
    
    
            return 2;
        }
        int left = dfs(root->left);
        int right = dfs(root->right);

        // 如果有子节点未被覆盖,则需要安摄像头
        if ((left == 0 && right == 0) || (left == 1 && right == 0) || (left == 0 && right == 1) || (left == 0 && right == 2) || (left == 2 && right == 0)) {
    
    
            ans += 1;
            return 1;
        }
        // 如果子节点可以覆盖此处,则返回已覆盖
        if ((left == 1 && right == 2) || (left == 2 && right == 1) || (left == 1 && right == 1)) {
    
    
            return 2;
        }
        // 如果子节点已被覆盖,则此处无需摄像头且未被覆盖
        if ((left == 2 && right == 2)) {
    
    
            return 0;
        }
        return 0;
    }

    int minCameraCover(TreeNode* root) {
    
    
        if (dfs(root) == 0) {
    
    
            ans += 1;
        }
        return ans;
    }
};

postscript

Ah, this is the code that I wa twice before. I pushed and pushed it and found it to be no problem, even perfect. I found out what was wrong I was like a mentally retarded QAQ
(but I can squat a question that only monitors child nodes hhhhhhhhhh

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    
    
public:
    int ans = 0;

    bool dfs(TreeNode *root) {
    
    
        while (root->left != nullptr || root->right != nullptr) {
    
    
            bool son1 = false, son2 = false;
            if (root->left == nullptr || (root->left->left == nullptr && root->left->right == nullptr)) {
    
    
                son1 = true;
            } else {
    
    
                if (dfs(root->left)) root->left = nullptr;
            }
            if (root->right == nullptr || (root->right->right == nullptr && root->right->left == nullptr)) {
    
    
                son2 = true;
            } else {
    
    
                if (dfs(root->right)) root->right = nullptr;
            }
            if (son1 && son2) {
    
    
                ans += 1;
                return true;
            }
        }
        return false;
    }
    
    int minCameraCover(TreeNode* root) {
    
    
        if (root -> left == nullptr && root -> right == nullptr) return 1;
        else if (root == nullptr) return 0;
        dfs(root);
        return ans;
    }
};

Guess you like

Origin blog.csdn.net/qq_45934120/article/details/108739932