2385. Amount of Time for Binary Tree to Be Infected Medium

@problem@discussion
#Tree#Depth-First Search#Breadth-First Search#Binary Tree



1/**
2 * [2385] Amount of Time for Binary Tree to Be Infected
3 *
4 * You are given the root of a binary tree with unique values, and an integer start. At minute 0, an infection starts from the node with value start.
5 * Each minute, a node becomes infected if:
6 * 
7 * 	The node is currently uninfected.
8 * 	The node is adjacent to an infected node.
9 * 
10 * Return the number of minutes needed for the entire tree to be infected.
11 *  
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
14 * Input: root = [1,5,3,null,4,10,6,9,2], start = 3
15 * Output: 4
16 * Explanation: The following nodes are infected during:
17 * - Minute 0: Node 3
18 * - Minute 1: Nodes 1, 10 and 6
19 * - Minute 2: Node 5
20 * - Minute 3: Node 4
21 * - Minute 4: Nodes 9 and 2
22 * It takes 4 minutes for the whole tree to be infected so we return 4.
23 * 
24 * Example 2:
25 * <img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
26 * Input: root = [1], start = 1
27 * Output: 0
28 * Explanation: At minute 0, the only node in the tree is infected so we return 0.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	The number of nodes in the tree is in the range [1, 10^5].
34 * 	1 <= Node.val <= 10^5
35 * 	Each node has a unique value.
36 * 	A node with a value of start exists in the tree.
37 * 
38 */
39pub struct Solution {}
40use crate::util::tree::{TreeNode, to_tree};
41
42// problem: https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/
43// discuss: https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47// Definition for a binary tree node.
48// #[derive(Debug, PartialEq, Eq)]
49// pub struct TreeNode {
50//   pub val: i32,
51//   pub left: Option<Rc<RefCell<TreeNode>>>,
52//   pub right: Option<Rc<RefCell<TreeNode>>>,
53// }
54// 
55// impl TreeNode {
56//   #[inline]
57//   pub fn new(val: i32) -> Self {
58//     TreeNode {
59//       val,
60//       left: None,
61//       right: None
62//     }
63//   }
64// }
65use std::rc::Rc;
66use std::cell::RefCell;
67impl Solution {
68    pub fn amount_of_time(root: Option<Rc<RefCell<TreeNode>>>, start: i32) -> i32 {
69        0
70    }
71}
72
73// submission codes end
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_2385() {
81    }
82}
83


Back
© 2025 bowen.ge All Rights Reserved.