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 e23ae77

Browse files
feat: add solutions to lc problem: No.2385 (doocs#2639)
No.2385.Amount of Time for Binary Tree to Be Infected
1 parent b81124b commit e23ae77

File tree

14 files changed

+294
-947
lines changed

14 files changed

+294
-947
lines changed

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README.md‎

Lines changed: 97 additions & 318 deletions
Large diffs are not rendered by default.

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/README_EN.md‎

Lines changed: 100 additions & 313 deletions
Large diffs are not rendered by default.

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution.cpp‎

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,29 @@
1111
*/
1212
class Solution {
1313
public:
14-
unordered_map<int, vector<int>> g;
15-
1614
int amountOfTime(TreeNode* root, int start) {
17-
dfs(root);
18-
queue<int> q{{start}};
19-
unordered_set<int> vis;
20-
int ans = -1;
21-
while (q.size()) {
22-
++ans;
23-
for (int n = q.size(); n; --n) {
24-
int i = q.front();
25-
q.pop();
26-
vis.insert(i);
27-
for (int j : g[i]) {
28-
if (!vis.count(j)) {
29-
q.push(j);
30-
}
15+
unordered_map<int, vector<int>> g;
16+
function<void(TreeNode*, TreeNode*)> dfs = [&](TreeNode* node, TreeNode* fa) {
17+
if (!node) {
18+
return;
19+
}
20+
if (fa) {
21+
g[node->val].push_back(fa->val);
22+
g[fa->val].push_back(node->val);
23+
}
24+
dfs(node->left, node);
25+
dfs(node->right, node);
26+
};
27+
function<int(int, int)> dfs2 = [&](int node, int fa) -> int {
28+
int ans = 0;
29+
for (int nxt : g[node]) {
30+
if (nxt != fa) {
31+
ans = max(ans, 1 + dfs2(nxt, node));
3132
}
3233
}
33-
}
34-
return ans;
35-
}
36-
37-
void dfs(TreeNode* root) {
38-
if (!root) return;
39-
if (root->left) {
40-
g[root->val].push_back(root->left->val);
41-
g[root->left->val].push_back(root->val);
42-
}
43-
if (root->right) {
44-
g[root->val].push_back(root->right->val);
45-
g[root->right->val].push_back(root->val);
46-
}
47-
dfs(root->left);
48-
dfs(root->right);
34+
return ans;
35+
};
36+
dfs(root, nullptr);
37+
return dfs2(start, -1);
4938
}
5039
};

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution.go‎

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,27 @@
88
*/
99
func amountOfTime(root *TreeNode, start int) int {
1010
g := map[int][]int{}
11-
var dfs func(*TreeNode)
12-
dfs = func(root *TreeNode) {
13-
if root == nil {
11+
var dfs func(*TreeNode, *TreeNode)
12+
dfs = func(node, fa *TreeNode) {
13+
if node == nil {
1414
return
1515
}
16-
if root.Left != nil {
17-
g[root.Val] = append(g[root.Val], root.Left.Val)
18-
g[root.Left.Val] = append(g[root.Left.Val], root.Val)
16+
if fa != nil {
17+
g[node.Val] = append(g[node.Val], fa.Val)
18+
g[fa.Val] = append(g[fa.Val], node.Val)
1919
}
20-
if root.Right != nil {
21-
g[root.Val] = append(g[root.Val], root.Right.Val)
22-
g[root.Right.Val] = append(g[root.Right.Val], root.Val)
23-
}
24-
dfs(root.Left)
25-
dfs(root.Right)
20+
dfs(node.Left, node)
21+
dfs(node.Right, node)
2622
}
27-
28-
dfs(root)
29-
q := []int{start}
30-
ans := -1
31-
vis := map[int]bool{}
32-
for len(q) > 0 {
33-
ans++
34-
for n := len(q); n > 0; n-- {
35-
i := q[0]
36-
q = q[1:]
37-
vis[i] = true
38-
for _, j := range g[i] {
39-
if !vis[j] {
40-
q = append(q, j)
41-
}
23+
var dfs2 func(int, int) int
24+
dfs2 = func(node, fa int) (ans int) {
25+
for _, nxt := range g[node] {
26+
if nxt != fa {
27+
ans = max(ans, 1+dfs2(nxt, node))
4228
}
4329
}
30+
return
4431
}
45-
return ans
32+
dfs(root, nil)
33+
return dfs2(start, -1)
4634
}

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution.java‎

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,29 @@ class Solution {
1717
private Map<Integer, List<Integer>> g = new HashMap<>();
1818

1919
public int amountOfTime(TreeNode root, int start) {
20-
dfs(root);
21-
Deque<Integer> q = new ArrayDeque<>();
22-
Set<Integer> vis = new HashSet<>();
23-
q.offer(start);
24-
int ans = -1;
25-
while (!q.isEmpty()) {
26-
++ans;
27-
for (int n = q.size(); n > 0; --n) {
28-
int i = q.pollFirst();
29-
vis.add(i);
30-
if (g.containsKey(i)) {
31-
for (int j : g.get(i)) {
32-
if (!vis.contains(j)) {
33-
q.offer(j);
34-
}
35-
}
36-
}
37-
}
38-
}
39-
return ans;
20+
dfs(root, null);
21+
return dfs2(start, -1);
4022
}
4123

42-
private void dfs(TreeNode root) {
43-
if (root == null) {
24+
private void dfs(TreeNode node, TreeNodefa) {
25+
if (node == null) {
4426
return;
4527
}
46-
if (root.left != null) {
47-
g.computeIfAbsent(root.val, k -> new ArrayList<>()).add(root.left.val);
48-
g.computeIfAbsent(root.left.val, k -> new ArrayList<>()).add(root.val);
28+
if (fa != null) {
29+
g.computeIfAbsent(node.val, k -> new ArrayList<>()).add(fa.val);
30+
g.computeIfAbsent(fa.val, k -> new ArrayList<>()).add(node.val);
4931
}
50-
if (root.right != null) {
51-
g.computeIfAbsent(root.val, k -> new ArrayList<>()).add(root.right.val);
52-
g.computeIfAbsent(root.right.val, k -> new ArrayList<>()).add(root.val);
32+
dfs(node.left, node);
33+
dfs(node.right, node);
34+
}
35+
36+
private int dfs2(int node, int fa) {
37+
int ans = 0;
38+
for (int nxt : g.getOrDefault(node, List.of())) {
39+
if (nxt != fa) {
40+
ans = Math.max(ans, 1 + dfs2(nxt, node));
41+
}
5342
}
54-
dfs(root.left);
55-
dfs(root.right);
43+
return ans;
5644
}
5745
}

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution.py‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@
66
# self.right = right
77
class Solution:
88
def amountOfTime(self, root: Optional[TreeNode], start: int) -> int:
9-
def dfs(root):
10-
if root is None:
9+
def dfs(node: Optional[TreeNode], fa: Optional[TreeNode]):
10+
if node is None:
1111
return
12-
if root.left:
13-
g[root.val].append(root.left.val)
14-
g[root.left.val].append(root.val)
15-
if root.right:
16-
g[root.val].append(root.right.val)
17-
g[root.right.val].append(root.val)
18-
dfs(root.left)
19-
dfs(root.right)
12+
if fa:
13+
g[node.val].append(fa.val)
14+
g[fa.val].append(node.val)
15+
dfs(node.left, node)
16+
dfs(node.right, node)
17+
18+
def dfs2(node: int, fa: int) -> int:
19+
ans = 0
20+
for nxt in g[node]:
21+
if nxt != fa:
22+
ans = max(ans, 1 + dfs2(nxt, node))
23+
return ans
2024

2125
g = defaultdict(list)
22-
dfs(root)
23-
vis = set()
24-
q = deque([start])
25-
ans = -1
26-
while q:
27-
ans += 1
28-
for _ in range(len(q)):
29-
i = q.popleft()
30-
vis.add(i)
31-
for j in g[i]:
32-
if j not in vis:
33-
q.append(j)
34-
return ans
26+
dfs(root, None)
27+
return dfs2(start, -1)

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution.ts‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313
*/
1414

1515
function amountOfTime(root: TreeNode | null, start: number): number {
16-
const map = new Map<number, number[]>();
17-
const create = ({ val, left, right }: TreeNode) => {
18-
if (left != null) {
19-
map.set(val, [...(map.get(val) ?? []), left.val]);
20-
map.set(left.val, [...(map.get(left.val) ?? []), val]);
21-
create(left);
16+
const g: Map<number, number[]> = new Map();
17+
const dfs = (node: TreeNode | null, fa: TreeNode | null) => {
18+
if (!node) {
19+
return;
2220
}
23-
if (right != null) {
24-
map.set(val, [...(map.get(val) ?? []), right.val]);
25-
map.set(right.val, [...(map.get(right.val) ?? []), val]);
26-
create(right);
21+
if (fa) {
22+
if (!g.has(node.val)) {
23+
g.set(node.val, []);
24+
}
25+
g.get(node.val)!.push(fa.val);
26+
if (!g.has(fa.val)) {
27+
g.set(fa.val, []);
28+
}
29+
g.get(fa.val)!.push(node.val);
2730
}
31+
dfs(node.left, node);
32+
dfs(node.right, node);
2833
};
29-
create(root);
30-
const dfs = (st: number, fa: number) => {
31-
let res = 0;
32-
for (const v of map.get(st) ?? []) {
33-
if (v !== fa) {
34-
res = Math.max(res, dfs(v, st) + 1);
34+
const dfs2 = (node: number, fa: number): number => {
35+
let ans = 0;
36+
for (const nxt of g.get(node) || []) {
37+
if (nxt !== fa) {
38+
ans = Math.max(ans, 1 + dfs2(nxt, node));
3539
}
3640
}
37-
return res;
41+
return ans;
3842
};
39-
return dfs(start, -1);
43+
dfs(root, null);
44+
return dfs2(start, -1);
4045
}

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution2.cpp‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎solution/2300-2399/2385.Amount of Time for Binary Tree to Be Infected/Solution2.go‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
(0)

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