Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 6bb4dfe

Browse files
committed
993 added
1 parent 9ed78f2 commit 6bb4dfe

File tree

7 files changed

+133
-21
lines changed

7 files changed

+133
-21
lines changed
14.8 KB
Loading
18.9 KB
Loading
14.6 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [993. Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)
2+
3+
In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
4+
5+
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
6+
7+
We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
8+
9+
Return true if and only if the nodes corresponding to the values x and y are cousins.
10+
11+
Example 1:
12+
13+
![1](1.png)
14+
15+
```text
16+
Input: root = [1,2,3,4], x = 4, y = 3
17+
Output: false
18+
```
19+
20+
Example 2:
21+
22+
![2](2.png)
23+
24+
```text
25+
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
26+
Output: true
27+
```
28+
29+
Example 3:
30+
31+
![3](3.png)
32+
33+
```text
34+
Input: root = [1,2,3,null,4], x = 2, y = 3
35+
Output: false
36+
```
37+
38+
Note:
39+
40+
- The number of nodes in the tree will be between 2 and 100.
41+
- Each node has a unique integer value from 1 to 100.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package problem0993
2+
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Go/kit"
5+
)
6+
7+
// TreeNode is Pre-defined....
8+
type TreeNode = kit.TreeNode
9+
10+
func isCousins(root *TreeNode, x int, y int) bool {
11+
12+
return false
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package problem0993
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
root []int
13+
x int
14+
y int
15+
ans bool
16+
}{
17+
18+
{
19+
[]int{1, 2, 3, 4},
20+
4,
21+
3,
22+
false,
23+
},
24+
25+
{
26+
[]int{1, 2, 3, kit.NULL, 4, kit.NULL, 5},
27+
5,
28+
4,
29+
true,
30+
},
31+
32+
{
33+
[]int{1, 2, 3, kit.NULL, 4},
34+
2,
35+
3,
36+
false,
37+
},
38+
39+
// 可以有多个 testcase
40+
}
41+
42+
func Test_isCousins(t *testing.T) {
43+
ast := assert.New(t)
44+
45+
for _, tc := range tcs {
46+
root := kit.Ints2TreeNode(tc.root)
47+
ast.Equal(tc.ans, isCousins(root, tc.x, tc.y), "输入:%v", tc)
48+
}
49+
}
50+
51+
func Benchmark_isCousins(b *testing.B) {
52+
for i := 0; i < b.N; i++ {
53+
for _, tc := range tcs {
54+
root := kit.Ints2TreeNode(tc.root)
55+
isCousins(root, tc.x, tc.y)
56+
}
57+
}
58+
}

leetcode.json

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 900,
4-
"Updated": "2019-05-08T21:50:22.795158005+08:00",
3+
"Ranking": 887,
4+
"Updated": "2019-05-09T19:41:54.815650961+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 226,
@@ -925,7 +925,7 @@
925925
"ID": 75,
926926
"Title": "Sort Colors",
927927
"TitleSlug": "sort-colors",
928-
"PassRate": "41%",
928+
"PassRate": "42%",
929929
"Difficulty": "Medium",
930930
"IsAccepted": true,
931931
"IsPaid": false,
@@ -1825,7 +1825,7 @@
18251825
"ID": 150,
18261826
"Title": "Evaluate Reverse Polish Notation",
18271827
"TitleSlug": "evaluate-reverse-polish-notation",
1828-
"PassRate": "31%",
1828+
"PassRate": "32%",
18291829
"Difficulty": "Medium",
18301830
"IsAccepted": true,
18311831
"IsPaid": false,
@@ -2449,7 +2449,7 @@
24492449
"ID": 202,
24502450
"Title": "Happy Number",
24512451
"TitleSlug": "happy-number",
2452-
"PassRate": "44%",
2452+
"PassRate": "45%",
24532453
"Difficulty": "Easy",
24542454
"IsAccepted": true,
24552455
"IsPaid": false,
@@ -2809,7 +2809,7 @@
28092809
"ID": 232,
28102810
"Title": "Implement Queue using Stacks",
28112811
"TitleSlug": "implement-queue-using-stacks",
2812-
"PassRate": "42%",
2812+
"PassRate": "43%",
28132813
"Difficulty": "Easy",
28142814
"IsAccepted": true,
28152815
"IsPaid": false,
@@ -3673,7 +3673,7 @@
36733673
"ID": 304,
36743674
"Title": "Range Sum Query 2D - Immutable",
36753675
"TitleSlug": "range-sum-query-2d-immutable",
3676-
"PassRate": "31%",
3676+
"PassRate": "32%",
36773677
"Difficulty": "Medium",
36783678
"IsAccepted": true,
36793679
"IsPaid": false,
@@ -5377,7 +5377,7 @@
53775377
"ID": 446,
53785378
"Title": "Arithmetic Slices II - Subsequence",
53795379
"TitleSlug": "arithmetic-slices-ii-subsequence",
5380-
"PassRate": "29%",
5380+
"PassRate": "30%",
53815381
"Difficulty": "Hard",
53825382
"IsAccepted": true,
53835383
"IsPaid": false,
@@ -7081,7 +7081,7 @@
70817081
"ID": 588,
70827082
"Title": "Design In-Memory File System",
70837083
"TitleSlug": "design-in-memory-file-system",
7084-
"PassRate": "38%",
7084+
"PassRate": "39%",
70857085
"Difficulty": "Hard",
70867086
"IsAccepted": false,
70877087
"IsPaid": true,
@@ -7609,7 +7609,7 @@
76097609
"ID": 632,
76107610
"Title": "Smallest Range",
76117611
"TitleSlug": "smallest-range",
7612-
"PassRate": "46%",
7612+
"PassRate": "47%",
76137613
"Difficulty": "Hard",
76147614
"IsAccepted": true,
76157615
"IsPaid": false,
@@ -8281,7 +8281,7 @@
82818281
"ID": 688,
82828282
"Title": "Knight Probability in Chessboard",
82838283
"TitleSlug": "knight-probability-in-chessboard",
8284-
"PassRate": "43%",
8284+
"PassRate": "44%",
82858285
"Difficulty": "Medium",
82868286
"IsAccepted": true,
82878287
"IsPaid": false,
@@ -8653,7 +8653,7 @@
86538653
"ID": 719,
86548654
"Title": "Find K-th Smallest Pair Distance",
86558655
"TitleSlug": "find-k-th-smallest-pair-distance",
8656-
"PassRate": "29%",
8656+
"PassRate": "28%",
86578657
"Difficulty": "Hard",
86588658
"IsAccepted": true,
86598659
"IsPaid": false,
@@ -8797,7 +8797,7 @@
87978797
"ID": 731,
87988798
"Title": "My Calendar II",
87998799
"TitleSlug": "my-calendar-ii",
8800-
"PassRate": "43%",
8800+
"PassRate": "44%",
88018801
"Difficulty": "Medium",
88028802
"IsAccepted": true,
88038803
"IsPaid": false,
@@ -10321,7 +10321,7 @@
1032110321
"ID": 858,
1032210322
"Title": "Mirror Reflection",
1032310323
"TitleSlug": "mirror-reflection",
10324-
"PassRate": "51%",
10324+
"PassRate": "52%",
1032510325
"Difficulty": "Medium",
1032610326
"IsAccepted": true,
1032710327
"IsPaid": false,
@@ -10585,7 +10585,7 @@
1058510585
"ID": 880,
1058610586
"Title": "Decoded String at Index",
1058710587
"TitleSlug": "decoded-string-at-index",
10588-
"PassRate": "22%",
10588+
"PassRate": "23%",
1058910589
"Difficulty": "Medium",
1059010590
"IsAccepted": true,
1059110591
"IsPaid": false,
@@ -11281,7 +11281,7 @@
1128111281
"ID": 938,
1128211282
"Title": "Range Sum of BST",
1128311283
"TitleSlug": "range-sum-of-bst",
11284-
"PassRate": "80%",
11284+
"PassRate": "79%",
1128511285
"Difficulty": "Easy",
1128611286
"IsAccepted": true,
1128711287
"IsPaid": false,
@@ -11509,7 +11509,7 @@
1150911509
"ID": 957,
1151011510
"Title": "Prison Cells After N Days",
1151111511
"TitleSlug": "prison-cells-after-n-days",
11512-
"PassRate": "37%",
11512+
"PassRate": "38%",
1151311513
"Difficulty": "Medium",
1151411514
"IsAccepted": true,
1151511515
"IsPaid": false,
@@ -11737,7 +11737,7 @@
1173711737
"ID": 976,
1173811738
"Title": "Largest Perimeter Triangle",
1173911739
"TitleSlug": "largest-perimeter-triangle",
11740-
"PassRate": "57%",
11740+
"PassRate": "56%",
1174111741
"Difficulty": "Easy",
1174211742
"IsAccepted": true,
1174311743
"IsPaid": false,
@@ -12457,7 +12457,7 @@
1245712457
"ID": 1036,
1245812458
"Title": "Escape a Large Maze",
1245912459
"TitleSlug": "escape-a-large-maze",
12460-
"PassRate": "41%",
12460+
"PassRate": "40%",
1246112461
"Difficulty": "Hard",
1246212462
"IsAccepted": false,
1246312463
"IsPaid": false,
@@ -12469,7 +12469,7 @@
1246912469
"ID": 1037,
1247012470
"Title": "Valid Boomerang",
1247112471
"TitleSlug": "valid-boomerang",
12472-
"PassRate": "37%",
12472+
"PassRate": "36%",
1247312473
"Difficulty": "Easy",
1247412474
"IsAccepted": false,
1247512475
"IsPaid": false,
@@ -12493,7 +12493,7 @@
1249312493
"ID": 1039,
1249412494
"Title": "Minimum Score Triangulation of Polygon",
1249512495
"TitleSlug": "minimum-score-triangulation-of-polygon",
12496-
"PassRate": "36%",
12496+
"PassRate": "37%",
1249712497
"Difficulty": "Medium",
1249812498
"IsAccepted": false,
1249912499
"IsPaid": false,

0 commit comments

Comments
 (0)