Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0629ee0

Browse files
feat: add No.2385
1 parent 6f07645 commit 0629ee0

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 2385. Amount of Time for Binary Tree to Be Infected
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree.
5+
- Similar Questions: Maximum Depth of Binary Tree, Shortest Path to Get Food, All Nodes Distance K in Binary Tree, Count the Number of Infection Sequences.
6+
7+
## Problem
8+
9+
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`.
10+
11+
Each minute, a node becomes infected if:
12+
13+
14+
15+
- The node is currently uninfected.
16+
17+
- The node is adjacent to an infected node.
18+
19+
20+
Return **the number of minutes needed for the entire tree to be infected.**
21+
22+
23+
Example 1:
24+
25+
![](https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png)
26+
27+
```
28+
Input: root = [1,5,3,null,4,10,6,9,2], start = 3
29+
Output: 4
30+
Explanation: The following nodes are infected during:
31+
- Minute 0: Node 3
32+
- Minute 1: Nodes 1, 10 and 6
33+
- Minute 2: Node 5
34+
- Minute 3: Node 4
35+
- Minute 4: Nodes 9 and 2
36+
It takes 4 minutes for the whole tree to be infected so we return 4.
37+
```
38+
39+
Example 2:
40+
41+
![](https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png)
42+
43+
```
44+
Input: root = [1], start = 1
45+
Output: 0
46+
Explanation: At minute 0, the only node in the tree is infected so we return 0.
47+
```
48+
49+
50+
**Constraints:**
51+
52+
53+
54+
- The number of nodes in the tree is in the range `[1, 105]`.
55+
56+
- `1 <= Node.val <= 105`
57+
58+
- Each node has a **unique** value.
59+
60+
- A node with a value of `start` exists in the tree.
61+
62+
63+
64+
## Solution
65+
66+
```javascript
67+
/**
68+
* Definition for a binary tree node.
69+
* function TreeNode(val, left, right) {
70+
* this.val = (val===undefined ? 0 : val)
71+
* this.left = (left===undefined ? null : left)
72+
* this.right = (right===undefined ? null : right)
73+
* }
74+
*/
75+
/**
76+
* @param {TreeNode} root
77+
* @param {number} start
78+
* @return {number}
79+
*/
80+
var amountOfTime = function(root, start) {
81+
const startNode = findStartNode(root, start, null);
82+
return findLongestPath(root, startNode, 0, {});
83+
};
84+
85+
var findStartNode = function(node, start, parent) {
86+
if (!node) return null;
87+
const startNode = (node.val === start ? node : null)
88+
|| findStartNode(node.left, start, node)
89+
|| findStartNode(node.right, start, node);
90+
if (startNode) {
91+
node.parent = parent;
92+
return startNode;
93+
}
94+
return null;
95+
};
96+
97+
var findLongestPath = function(root, node, depth, visited) {
98+
if (!node || visited[node.val]) return 0;
99+
visited[node.val] = true;
100+
if (!node.left && !node.right && !node.parent) return depth;
101+
return Math.max(
102+
node === root ? depth : 0,
103+
findLongestPath(root, node.left, depth + 1, visited),
104+
findLongestPath(root, node.right, depth + 1, visited),
105+
findLongestPath(root, node.parent, depth + 1, visited),
106+
);
107+
};
108+
```
109+
110+
**Explain:**
111+
112+
nope.
113+
114+
**Complexity:**
115+
116+
* Time complexity : O(n).
117+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /