|
58 | 58 |
|
59 | 59 | 先通过 $DFS$ 建图,得到 $g$。然后以 $start$ 作为起点,哈希表 $vis$ 标记访问过的节点,通过 $BFS$ 以及前面得到的图 $g$,逐层往外扩展,扩展的次数即为答案。
|
60 | 60 |
|
61 |
| -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 |
| 61 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 |
| 62 | + |
| 63 | +**方法二:两次 DFS** |
| 64 | + |
| 65 | +与方法一一样,我们先通过 $DFS$ 建图,得到 $g$。然后以 $start$ 作为起点,通过 $DFS$ 搜索整棵树,找到最远距离,即为答案。 |
| 66 | + |
| 67 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 |
62 | 68 |
|
63 | 69 | <!-- tabs:start -->
|
64 | 70 |
|
@@ -103,6 +109,39 @@ class Solution:
|
103 | 109 | return ans
|
104 | 110 | ```
|
105 | 111 |
|
| 112 | +```python |
| 113 | +# Definition for a binary tree node. |
| 114 | +# class TreeNode: |
| 115 | +# def __init__(self, val=0, left=None, right=None): |
| 116 | +# self.val = val |
| 117 | +# self.left = left |
| 118 | +# self.right = right |
| 119 | +class Solution: |
| 120 | + def amountOfTime(self, root: Optional[TreeNode], start: int) -> int: |
| 121 | + def dfs(root): |
| 122 | + if root is None: |
| 123 | + return |
| 124 | + if root.left: |
| 125 | + g[root.val].append(root.left.val) |
| 126 | + g[root.left.val].append(root.val) |
| 127 | + if root.right: |
| 128 | + g[root.val].append(root.right.val) |
| 129 | + g[root.right.val].append(root.val) |
| 130 | + dfs(root.left) |
| 131 | + dfs(root.right) |
| 132 | + |
| 133 | + def dfs2(i, fa): |
| 134 | + ans = 0 |
| 135 | + for j in g[i]: |
| 136 | + if j != fa: |
| 137 | + ans = max(ans, 1 + dfs2(j, i)) |
| 138 | + return ans |
| 139 | + |
| 140 | + g = defaultdict(list) |
| 141 | + dfs(root) |
| 142 | + return dfs2(start, -1) |
| 143 | +``` |
| 144 | + |
106 | 145 | ### **Java**
|
107 | 146 |
|
108 | 147 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
@@ -167,6 +206,58 @@ class Solution {
|
167 | 206 | }
|
168 | 207 | ```
|
169 | 208 |
|
| 209 | +```java |
| 210 | +/** |
| 211 | + * Definition for a binary tree node. |
| 212 | + * public class TreeNode { |
| 213 | + * int val; |
| 214 | + * TreeNode left; |
| 215 | + * TreeNode right; |
| 216 | + * TreeNode() {} |
| 217 | + * TreeNode(int val) { this.val = val; } |
| 218 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 219 | + * this.val = val; |
| 220 | + * this.left = left; |
| 221 | + * this.right = right; |
| 222 | + * } |
| 223 | + * } |
| 224 | + */ |
| 225 | +class Solution { |
| 226 | + private Map<Integer, List<Integer>> g = new HashMap<>(); |
| 227 | + |
| 228 | + public int amountOfTime(TreeNode root, int start) { |
| 229 | + dfs(root); |
| 230 | + return dfs(start, -1); |
| 231 | + } |
| 232 | + |
| 233 | + private int dfs(int i, int fa) { |
| 234 | + int ans = 0; |
| 235 | + for (int j : g.getOrDefault(i, Collections.emptyList())) { |
| 236 | + if (j != fa) { |
| 237 | + ans = Math.max(ans, 1 + dfs(j, i)); |
| 238 | + } |
| 239 | + } |
| 240 | + return ans; |
| 241 | + } |
| 242 | + |
| 243 | + private void dfs(TreeNode root) { |
| 244 | + if (root == null) { |
| 245 | + return; |
| 246 | + } |
| 247 | + if (root.left != null) { |
| 248 | + g.computeIfAbsent(root.left.val, k -> new ArrayList<>()).add(root.val); |
| 249 | + g.computeIfAbsent(root.val, k -> new ArrayList<>()).add(root.left.val); |
| 250 | + } |
| 251 | + if (root.right != null) { |
| 252 | + g.computeIfAbsent(root.right.val, k -> new ArrayList<>()).add(root.val); |
| 253 | + g.computeIfAbsent(root.val, k -> new ArrayList<>()).add(root.right.val); |
| 254 | + } |
| 255 | + dfs(root.left); |
| 256 | + dfs(root.right); |
| 257 | + } |
| 258 | +} |
| 259 | +``` |
| 260 | + |
170 | 261 | ### **C++**
|
171 | 262 |
|
172 | 263 | ```cpp
|
@@ -222,6 +313,53 @@ public:
|
222 | 313 | };
|
223 | 314 | ```
|
224 | 315 |
|
| 316 | +```cpp |
| 317 | +/** |
| 318 | + * Definition for a binary tree node. |
| 319 | + * struct TreeNode { |
| 320 | + * int val; |
| 321 | + * TreeNode *left; |
| 322 | + * TreeNode *right; |
| 323 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 324 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 325 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 326 | + * }; |
| 327 | + */ |
| 328 | +class Solution { |
| 329 | +public: |
| 330 | + unordered_map<int, vector<int>> g; |
| 331 | +
|
| 332 | + int amountOfTime(TreeNode* root, int start) { |
| 333 | + dfs(root); |
| 334 | + return dfs(start, -1); |
| 335 | + } |
| 336 | +
|
| 337 | + int dfs(int i, int fa) { |
| 338 | + int ans = 0; |
| 339 | + for (int& j : g[i]) { |
| 340 | + if (j != fa) { |
| 341 | + ans = max(ans, 1 + dfs(j, i)); |
| 342 | + } |
| 343 | + } |
| 344 | + return ans; |
| 345 | + } |
| 346 | +
|
| 347 | + void dfs(TreeNode* root) { |
| 348 | + if (!root) return; |
| 349 | + if (root->left) { |
| 350 | + g[root->val].push_back(root->left->val); |
| 351 | + g[root->left->val].push_back(root->val); |
| 352 | + } |
| 353 | + if (root->right) { |
| 354 | + g[root->val].push_back(root->right->val); |
| 355 | + g[root->right->val].push_back(root->val); |
| 356 | + } |
| 357 | + dfs(root->left); |
| 358 | + dfs(root->right); |
| 359 | + } |
| 360 | +}; |
| 361 | +``` |
| 362 | + |
225 | 363 | ### **Go**
|
226 | 364 |
|
227 | 365 | ```go
|
@@ -273,6 +411,57 @@ func amountOfTime(root *TreeNode, start int) int {
|
273 | 411 | }
|
274 | 412 | ```
|
275 | 413 |
|
| 414 | +```go |
| 415 | +/** |
| 416 | + * Definition for a binary tree node. |
| 417 | + * type TreeNode struct { |
| 418 | + * Val int |
| 419 | + * Left *TreeNode |
| 420 | + * Right *TreeNode |
| 421 | + * } |
| 422 | + */ |
| 423 | +func amountOfTime(root *TreeNode, start int) int { |
| 424 | + g := map[int][]int{} |
| 425 | + var dfs func(*TreeNode) |
| 426 | + dfs = func(root *TreeNode) { |
| 427 | + if root == nil { |
| 428 | + return |
| 429 | + } |
| 430 | + if root.Left != nil { |
| 431 | + g[root.Val] = append(g[root.Val], root.Left.Val) |
| 432 | + g[root.Left.Val] = append(g[root.Left.Val], root.Val) |
| 433 | + } |
| 434 | + if root.Right != nil { |
| 435 | + g[root.Val] = append(g[root.Val], root.Right.Val) |
| 436 | + g[root.Right.Val] = append(g[root.Right.Val], root.Val) |
| 437 | + } |
| 438 | + dfs(root.Left) |
| 439 | + dfs(root.Right) |
| 440 | + } |
| 441 | + |
| 442 | + var dfs2 func(int, int) int |
| 443 | + dfs2 = func(i, fa int) int { |
| 444 | + ans := 0 |
| 445 | + for _, j := range g[i] { |
| 446 | + if j != fa { |
| 447 | + ans = max(ans, 1+dfs2(j, i)) |
| 448 | + } |
| 449 | + } |
| 450 | + return ans |
| 451 | + } |
| 452 | + |
| 453 | + dfs(root) |
| 454 | + return dfs2(start, -1) |
| 455 | +} |
| 456 | + |
| 457 | +func max(a, b int) int { |
| 458 | + if a > b { |
| 459 | + return a |
| 460 | + } |
| 461 | + return b |
| 462 | +} |
| 463 | +``` |
| 464 | + |
276 | 465 | ### **TypeScript**
|
277 | 466 |
|
278 | 467 | ```ts
|
|
0 commit comments