【每日一题】Leetcode - 968. Binary Tree Cameras

Question

Leetcode - 968. Binary Tree Cameras

Train of thought

  • Using postorder traversal and greedy algorithm;
    • We define three states for node:
      • NO_COVERED: indicate the current node and its immediate nodes do not install camera;
      • INSTALL_CAMERA: indicate the current node install camera
      • HAS_COVERED: indicate the current node’s immediate nodes has install camera at least one node
    • Especial for the null node, we think it’s state is HAS_COVERED.
  • What is the greedy strategy?
    • Now give you three immediate node, names parent, left child and right child, and the left child and right child 's state is certain
    • We can judge the children’s states to sure the parent node’s state:
      • left == HAS_COVERED && right == HAS_COVERED: the parent node’s state is NO_COVERED;
      • left == NO_COVERED || right == NO_COVERED: the parent node’s state is INSTALL_CAMERA;
      • other: the parent node’s state is HAS_COVERED.
    • Especial action is we need to count of the node that its state is INSTALL_CAMERA.
  • Last, we using post order traversal to sure happened the child node’s state be determined before the parent node.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    private final int NO_COVERED = 0; // indicate the current node and its immediate nodes do not install camera
    private final int INSTALL_CAMERA = 1; // indicate the current node install camera
    private final int HAS_COVERED = 2; // indicate the current node's immediate nodes has install camera at least one node

    private int camears; // the count of install cameras

    public int minCameraCover(TreeNode root) {
        camears = 0;
        if (postTraversal(root) == NO_COVERED) {
            camears++;
        }
        return camears;
    }

    private int postTraversal(TreeNode root) {
        if (root == null) {
            return HAS_COVERED;
        }

        int left = postTraversal(root.left);
        int right = postTraversal(root.right);

        if (left == HAS_COVERED && right == HAS_COVERED) {
            return NO_COVERED;
        }

        if (left == NO_COVERED || right == NO_COVERED) {
            camears++;
            return INSTALL_CAMERA;
        }

        //  other states:
        //      HAS_COVERED && INSTALL_CAMERA
        //      INSTALL_CAMERA && INSTALL_CAMERA
        //  above example states shows the root surely be covered
        return HAS_COVERED;
    }

}

在这里插入图片描述

Optimize

nothing

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值