968. Binary Tree Cameras Hard

@problem@discussion
#Dynamic Programming#Tree#Depth-First Search#Binary Tree



1/**
2 * [968] Binary Tree Cameras
3 *
4 * You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.
5 * Return the minimum number of cameras needed to monitor all nodes of the tree.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png" style="width: 138px; height: 163px;" />
9 * Input: root = [0,0,null,0,0]
10 * Output: 1
11 * Explanation: One camera is enough to monitor all nodes if placed as shown.
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png" style="width: 139px; height: 312px;" />
15 * Input: root = [0,0,null,0,null,0,null,null,0]
16 * Output: 2
17 * Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	The number of nodes in the tree is in the range [1, 1000].
23 * 	Node.val == 0
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/binary-tree-cameras/
30// discuss: https://leetcode.com/problems/binary-tree-cameras/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn min_camera_cover(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_968() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.