Skip to content

Commit 9d0c5be

Browse files
committed
feat: add solutions to lc problem: No.0993
No.0993.Cousins in Binary Tree
1 parent 702d3e0 commit 9d0c5be

File tree

2 files changed

+343
-1
lines changed

2 files changed

+343
-1
lines changed

solution/0900-0999/0993.Cousins in Binary Tree/README.md

+173-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,20 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
BFS 实现。可以利用数组 p, d 记录每个节点对应的父节点以及深度。
58+
**1. BFS 实现**
59+
60+
可以利用数组 p, d 记录每个节点对应的父节点以及深度。
61+
62+
**2. DFS 实现**
5963

6064
<!-- tabs:start -->
6165

6266
### **Python3**
6367

6468
<!-- 这里可写当前语言的特殊实现逻辑 -->
6569

70+
BFS:
71+
6672
```python
6773
# Definition for a binary tree node.
6874
# class TreeNode:
@@ -91,10 +97,40 @@ class Solution:
9197
return p[x] != p[y] and d[x] == d[y]
9298
```
9399

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+
94128
### **Java**
95129

96130
<!-- 这里可写当前语言的特殊实现逻辑 -->
97131

132+
BFS:
133+
98134
```java
99135
/**
100136
* Definition for a binary tree node.
@@ -139,8 +175,58 @@ class Solution {
139175
}
140176
```
141177

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+
142226
### **C++**
143227

228+
BFS:
229+
144230
```cpp
145231
/**
146232
* Definition for a binary tree node->
@@ -187,8 +273,56 @@ public:
187273
};
188274
```
189275
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+
190322
### **Go**
191323

324+
BFS:
325+
192326
```go
193327
/**
194328
* Definition for a binary tree node.
@@ -226,6 +360,44 @@ func isCousins(root *TreeNode, x int, y int) bool {
226360
}
227361
```
228362

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+
229401
### **...**
230402

231403
```

0 commit comments

Comments
 (0)