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 1b5b4a7

Browse files
committed
feat: add solutions to lc problem: No.0993.Cousins in Binary Tree
1 parent 7b429b0 commit 1b5b4a7

File tree

6 files changed

+449
-4
lines changed

6 files changed

+449
-4
lines changed

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

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,179 @@
5151

5252
<p> </p>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
5857

58+
BFS 实现。可以利用数组 p, d 记录每个节点对应的父节点以及深度。
59+
5960
<!-- tabs:start -->
6061

6162
### **Python3**
6263

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

6566
```python
66-
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
75+
p = list(range(110))
76+
d = list(range(110))
77+
q = collections.deque([root])
78+
i = 0
79+
while q:
80+
n = len(q)
81+
for _ in range(n):
82+
node = q.popleft()
83+
d[node.val] = i
84+
if node.left:
85+
p[node.left.val] = node.val
86+
q.append(node.left)
87+
if node.right:
88+
q.append(node.right)
89+
p[node.right.val] = node.val
90+
i += 1
91+
return p[x] != p[y] and d[x] == d[y]
6792
```
6893

6994
### **Java**
7095

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

7398
```java
99+
/**
100+
* Definition for a binary tree node.
101+
* public class TreeNode {
102+
* int val;
103+
* TreeNode left;
104+
* TreeNode right;
105+
* TreeNode() {}
106+
* TreeNode(int val) { this.val = val; }
107+
* TreeNode(int val, TreeNode left, TreeNode right) {
108+
* this.val = val;
109+
* this.left = left;
110+
* this.right = right;
111+
* }
112+
* }
113+
*/
114+
class Solution {
115+
public boolean isCousins(TreeNode root, int x, int y) {
116+
int[] p = new int[110];
117+
int[] d = new int[110];
118+
Deque<TreeNode> q = new ArrayDeque<>();
119+
q.offer(root);
120+
int i = 0;
121+
while (!q.isEmpty()) {
122+
int n = q.size();
123+
while (n-- > 0) {
124+
TreeNode node = q.poll();
125+
d[node.val] = i;
126+
if (node.left != null) {
127+
q.offer(node.left);
128+
p[node.left.val] = node.val;
129+
}
130+
if (node.right != null) {
131+
q.offer(node.right);
132+
p[node.right.val] = node.val;
133+
}
134+
}
135+
++i;
136+
}
137+
return p[x] != p[y] && d[x] == d[y];
138+
}
139+
}
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
/**
146+
* Definition for a binary tree node->
147+
* struct TreeNode {
148+
* int val;
149+
* TreeNode *left;
150+
* TreeNode *right;
151+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
152+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
153+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
154+
* };
155+
*/
156+
class Solution {
157+
public:
158+
bool isCousins(TreeNode* root, int x, int y) {
159+
vector<int> p(110);
160+
vector<int> d(110);
161+
queue<TreeNode*> q;
162+
q.push(root);
163+
int i = 0;
164+
while (!q.empty())
165+
{
166+
int n = q.size();
167+
while (n--)
168+
{
169+
auto node = q.front();
170+
d[node->val] = i;
171+
q.pop();
172+
if (node->left)
173+
{
174+
q.push(node->left);
175+
p[node->left->val] = node->val;
176+
}
177+
if (node->right)
178+
{
179+
q.push(node->right);
180+
p[node->right->val] = node->val;
181+
}
182+
}
183+
++i;
184+
}
185+
return p[x] != p[y] && d[x] == d[y];
186+
}
187+
};
188+
```
74189
190+
### **Go**
191+
192+
```go
193+
/**
194+
* Definition for a binary tree node.
195+
* type TreeNode struct {
196+
* Val int
197+
* Left *TreeNode
198+
* Right *TreeNode
199+
* }
200+
*/
201+
func isCousins(root *TreeNode, x int, y int) bool {
202+
p := make([]int, 110)
203+
d := make([]int, 110)
204+
var q []*TreeNode
205+
q = append(q, root)
206+
i := 0
207+
for len(q) > 0 {
208+
n := len(q)
209+
for n > 0 {
210+
node := q[0]
211+
q = q[1:]
212+
n--
213+
d[node.Val] = i
214+
if node.Left != nil {
215+
q = append(q, node.Left)
216+
p[node.Left.Val] = node.Val
217+
}
218+
if node.Right != nil {
219+
q = append(q, node.Right)
220+
p[node.Right.Val] = node.Val
221+
}
222+
}
223+
i++
224+
}
225+
return p[x] != p[y] && d[x] == d[y]
226+
}
75227
```
76228

77229
### **...**

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

Lines changed: 152 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,171 @@
5151
<li>Each node has a unique integer value from <code>1</code> to <code>100</code>.</li>
5252
</ul>
5353

54-
5554
## Solutions
5655

5756
<!-- tabs:start -->
5857

5958
### **Python3**
6059

6160
```python
62-
61+
# Definition for a binary tree node.
62+
# class TreeNode:
63+
# def __init__(self, val=0, left=None, right=None):
64+
# self.val = val
65+
# self.left = left
66+
# self.right = right
67+
class Solution:
68+
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
69+
p = list(range(110))
70+
d = list(range(110))
71+
q = collections.deque([root])
72+
i = 0
73+
while q:
74+
n = len(q)
75+
for _ in range(n):
76+
node = q.popleft()
77+
d[node.val] = i
78+
if node.left:
79+
p[node.left.val] = node.val
80+
q.append(node.left)
81+
if node.right:
82+
q.append(node.right)
83+
p[node.right.val] = node.val
84+
i += 1
85+
return p[x] != p[y] and d[x] == d[y]
6386
```
6487

6588
### **Java**
6689

6790
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
public boolean isCousins(TreeNode root, int x, int y) {
108+
int[] p = new int[110];
109+
int[] d = new int[110];
110+
Deque<TreeNode> q = new ArrayDeque<>();
111+
q.offer(root);
112+
int i = 0;
113+
while (!q.isEmpty()) {
114+
int n = q.size();
115+
while (n-- > 0) {
116+
TreeNode node = q.poll();
117+
d[node.val] = i;
118+
if (node.left != null) {
119+
q.offer(node.left);
120+
p[node.left.val] = node.val;
121+
}
122+
if (node.right != null) {
123+
q.offer(node.right);
124+
p[node.right.val] = node.val;
125+
}
126+
}
127+
++i;
128+
}
129+
return p[x] != p[y] && d[x] == d[y];
130+
}
131+
}
132+
```
133+
134+
### **C++**
135+
136+
```cpp
137+
/**
138+
* Definition for a binary tree node->
139+
* struct TreeNode {
140+
* int val;
141+
* TreeNode *left;
142+
* TreeNode *right;
143+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
144+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
145+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
146+
* };
147+
*/
148+
class Solution {
149+
public:
150+
bool isCousins(TreeNode* root, int x, int y) {
151+
vector<int> p(110);
152+
vector<int> d(110);
153+
queue<TreeNode*> q;
154+
q.push(root);
155+
int i = 0;
156+
while (!q.empty())
157+
{
158+
int n = q.size();
159+
while (n--)
160+
{
161+
auto node = q.front();
162+
d[node->val] = i;
163+
q.pop();
164+
if (node->left)
165+
{
166+
q.push(node->left);
167+
p[node->left->val] = node->val;
168+
}
169+
if (node->right)
170+
{
171+
q.push(node->right);
172+
p[node->right->val] = node->val;
173+
}
174+
}
175+
++i;
176+
}
177+
return p[x] != p[y] && d[x] == d[y];
178+
}
179+
};
180+
```
68181
182+
### **Go**
183+
184+
```go
185+
/**
186+
* Definition for a binary tree node.
187+
* type TreeNode struct {
188+
* Val int
189+
* Left *TreeNode
190+
* Right *TreeNode
191+
* }
192+
*/
193+
func isCousins(root *TreeNode, x int, y int) bool {
194+
p := make([]int, 110)
195+
d := make([]int, 110)
196+
var q []*TreeNode
197+
q = append(q, root)
198+
i := 0
199+
for len(q) > 0 {
200+
n := len(q)
201+
for n > 0 {
202+
node := q[0]
203+
q = q[1:]
204+
n--
205+
d[node.Val] = i
206+
if node.Left != nil {
207+
q = append(q, node.Left)
208+
p[node.Left.Val] = node.Val
209+
}
210+
if node.Right != nil {
211+
q = append(q, node.Right)
212+
p[node.Right.Val] = node.Val
213+
}
214+
}
215+
i++
216+
}
217+
return p[x] != p[y] && d[x] == d[y]
218+
}
69219
```
70220

71221
### **...**

0 commit comments

Comments
(0)

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