|
55 | 55 |
|
56 | 56 | <!-- 这里可写通用的实现逻辑 -->
|
57 | 57 |
|
58 |
| -BFS 实现。可以利用数组 p, d 记录每个节点对应的父节点以及深度。 |
| 58 | +**1. BFS 实现** |
| 59 | + |
| 60 | +可以利用数组 p, d 记录每个节点对应的父节点以及深度。 |
| 61 | + |
| 62 | +**2. DFS 实现** |
59 | 63 |
|
60 | 64 | <!-- tabs:start -->
|
61 | 65 |
|
62 | 66 | ### **Python3**
|
63 | 67 |
|
64 | 68 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 69 |
|
| 70 | +BFS: |
| 71 | + |
66 | 72 | ```python
|
67 | 73 | # Definition for a binary tree node.
|
68 | 74 | # class TreeNode:
|
@@ -91,10 +97,40 @@ class Solution:
|
91 | 97 | return p[x] != p[y] and d[x] == d[y]
|
92 | 98 | ```
|
93 | 99 |
|
| 100 | +DFS: |
| 101 | + |
| 102 | +```python |
| 103 | +# Definition for a binary tree node. |
| 104 | +# class TreeNode: |
| 105 | +# def __init__(self, val=0, left=None, right=None): |
| 106 | +# self.val = val |
| 107 | +# self.left = left |
| 108 | +# self.right = right |
| 109 | +class Solution: |
| 110 | + def isCousins(self, root: TreeNode, x: int, y: int) -> bool: |
| 111 | + p1 = p2 = d1 = d2 = None |
| 112 | + |
| 113 | + def dfs(root, p, d): |
| 114 | + if root is None: |
| 115 | + return |
| 116 | + nonlocal p1, p2, d1, d2, x, y |
| 117 | + if root.val == x: |
| 118 | + p1, d1 = p, d |
| 119 | + if root.val == y: |
| 120 | + p2, d2 = p, d |
| 121 | + dfs(root.left, root, d + 1) |
| 122 | + dfs(root.right, root, d + 1) |
| 123 | + |
| 124 | + dfs(root, None, 0) |
| 125 | + return p1 != p2 and d1 == d2 |
| 126 | +``` |
| 127 | + |
94 | 128 | ### **Java**
|
95 | 129 |
|
96 | 130 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
97 | 131 |
|
| 132 | +BFS: |
| 133 | + |
98 | 134 | ```java
|
99 | 135 | /**
|
100 | 136 | * Definition for a binary tree node.
|
@@ -139,8 +175,58 @@ class Solution {
|
139 | 175 | }
|
140 | 176 | ```
|
141 | 177 |
|
| 178 | +DFS: |
| 179 | + |
| 180 | +```java |
| 181 | +/** |
| 182 | + * Definition for a binary tree node. |
| 183 | + * public class TreeNode { |
| 184 | + * int val; |
| 185 | + * TreeNode left; |
| 186 | + * TreeNode right; |
| 187 | + * TreeNode() {} |
| 188 | + * TreeNode(int val) { this.val = val; } |
| 189 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 190 | + * this.val = val; |
| 191 | + * this.left = left; |
| 192 | + * this.right = right; |
| 193 | + * } |
| 194 | + * } |
| 195 | + */ |
| 196 | +class Solution { |
| 197 | + private int x, y; |
| 198 | + private TreeNode p1, p2; |
| 199 | + private int d1, d2; |
| 200 | + |
| 201 | + public boolean isCousins(TreeNode root, int x, int y) { |
| 202 | + this.x = x; |
| 203 | + this.y = y; |
| 204 | + dfs(root, null, 0); |
| 205 | + return p1 != p2 && d1 == d2; |
| 206 | + } |
| 207 | + |
| 208 | + private void dfs(TreeNode root, TreeNode p, int d) { |
| 209 | + if (root == null) { |
| 210 | + return; |
| 211 | + } |
| 212 | + if (root.val == x) { |
| 213 | + p1 = p; |
| 214 | + d1 = d; |
| 215 | + } |
| 216 | + if (root.val == y) { |
| 217 | + p2 = p; |
| 218 | + d2 = d; |
| 219 | + } |
| 220 | + dfs(root.left, root, d + 1); |
| 221 | + dfs(root.right, root, d + 1); |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
| 225 | + |
142 | 226 | ### **C++**
|
143 | 227 |
|
| 228 | +BFS: |
| 229 | + |
144 | 230 | ```cpp
|
145 | 231 | /**
|
146 | 232 | * Definition for a binary tree node->
|
@@ -187,8 +273,56 @@ public:
|
187 | 273 | };
|
188 | 274 | ```
|
189 | 275 |
|
| 276 | +DFS: |
| 277 | +
|
| 278 | +```cpp |
| 279 | +/** |
| 280 | + * Definition for a binary tree node. |
| 281 | + * struct TreeNode { |
| 282 | + * int val; |
| 283 | + * TreeNode *left; |
| 284 | + * TreeNode *right; |
| 285 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 286 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 287 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 288 | + * }; |
| 289 | + */ |
| 290 | +class Solution { |
| 291 | +public: |
| 292 | + TreeNode* p1; |
| 293 | + TreeNode* p2; |
| 294 | + int d1, d2; |
| 295 | + int x, y; |
| 296 | +
|
| 297 | + bool isCousins(TreeNode* root, int x, int y) { |
| 298 | + this->x = x; |
| 299 | + this->y = y; |
| 300 | + dfs(root, nullptr, 0); |
| 301 | + return p1 != p2 && d1 == d2; |
| 302 | + } |
| 303 | +
|
| 304 | + void dfs(TreeNode* root, TreeNode* p, int d) { |
| 305 | + if (!root) return; |
| 306 | + if (root->val == x) |
| 307 | + { |
| 308 | + p1 = p; |
| 309 | + d1 = d; |
| 310 | + } |
| 311 | + if (root->val == y) |
| 312 | + { |
| 313 | + p2 = p; |
| 314 | + d2 = d; |
| 315 | + } |
| 316 | + dfs(root->left, root, d + 1); |
| 317 | + dfs(root->right, root, d + 1); |
| 318 | + } |
| 319 | +}; |
| 320 | +``` |
| 321 | + |
190 | 322 | ### **Go**
|
191 | 323 |
|
| 324 | +BFS: |
| 325 | + |
192 | 326 | ```go
|
193 | 327 | /**
|
194 | 328 | * Definition for a binary tree node.
|
@@ -226,6 +360,44 @@ func isCousins(root *TreeNode, x int, y int) bool {
|
226 | 360 | }
|
227 | 361 | ```
|
228 | 362 |
|
| 363 | +DFS: |
| 364 | + |
| 365 | +```go |
| 366 | +/** |
| 367 | + * Definition for a binary tree node. |
| 368 | + * type TreeNode struct { |
| 369 | + * Val int |
| 370 | + * Left *TreeNode |
| 371 | + * Right *TreeNode |
| 372 | + * } |
| 373 | + */ |
| 374 | +var p1 *TreeNode |
| 375 | +var p2 *TreeNode |
| 376 | +var d1 int |
| 377 | +var d2 int |
| 378 | + |
| 379 | +func isCousins(root *TreeNode, x int, y int) bool { |
| 380 | + dfs(root, nil, x, y, 0) |
| 381 | + return p1 != p2 && d1 == d2 |
| 382 | +} |
| 383 | + |
| 384 | +func dfs(root, p *TreeNode, x, y, d int) { |
| 385 | + if root == nil { |
| 386 | + return |
| 387 | + } |
| 388 | + if root.Val == x { |
| 389 | + p1 = p |
| 390 | + d1 = d |
| 391 | + } |
| 392 | + if root.Val == y { |
| 393 | + p2 = p |
| 394 | + d2 = d |
| 395 | + } |
| 396 | + dfs(root.Left, root, x, y, d+1) |
| 397 | + dfs(root.Right, root, x, y, d+1) |
| 398 | +} |
| 399 | +``` |
| 400 | + |
229 | 401 | ### **...**
|
230 | 402 |
|
231 | 403 | ```
|
|
0 commit comments