Skip to content

Commit a54c439

Browse files
committed
1104 added
1 parent c49a84b commit a54c439

File tree

5 files changed

+96
-21
lines changed

5 files changed

+96
-21
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [1104. Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/)
2+
3+
In an infinite binary tree where every node has two children, the nodes are labelled in row order.
4+
5+
In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.
6+
7+
![tree](tree.png)
8+
9+
Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.
10+
11+
Example 1:
12+
13+
```text
14+
Input: label = 14
15+
Output: [1,3,4,14]
16+
```
17+
18+
Example 2:
19+
20+
```text
21+
Input: label = 26
22+
Output: [1,2,6,10,26]
23+
```
24+
25+
Constraints:
26+
27+
1. `1 <= label <= 10^6`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem1104
2+
3+
func pathInZigZagTree(label int) []int {
4+
5+
return nil
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package problem1104
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
label int
12+
ans []int
13+
}{
14+
15+
{
16+
14,
17+
[]int{1, 3, 4, 14},
18+
},
19+
20+
{
21+
26,
22+
[]int{1, 2, 6, 10, 26},
23+
},
24+
25+
// 可以有多个 testcase
26+
}
27+
28+
func Test_pathInZigZagTree(t *testing.T) {
29+
ast := assert.New(t)
30+
31+
for _, tc := range tcs {
32+
ast.Equal(tc.ans, pathInZigZagTree(tc.label), "输入:%v", tc)
33+
}
34+
}
35+
36+
func Benchmark_pathInZigZagTree(b *testing.B) {
37+
for i := 0; i < b.N; i++ {
38+
for _, tc := range tcs {
39+
pathInZigZagTree(tc.label)
40+
}
41+
}
42+
}
52.4 KB
Loading

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": 556,
4-
"Updated": "2019-07-22T17:27:30.448355038+08:00",
3+
"Ranking": 559,
4+
"Updated": "2019-07-23T19:58:14.018843795+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 253,
@@ -241,7 +241,7 @@
241241
"ID": 18,
242242
"Title": "4Sum",
243243
"TitleSlug": "4sum",
244-
"PassRate": "30%",
244+
"PassRate": "31%",
245245
"Difficulty": "Medium",
246246
"IsAccepted": true,
247247
"IsPaid": false,
@@ -1105,7 +1105,7 @@
11051105
"ID": 90,
11061106
"Title": "Subsets II",
11071107
"TitleSlug": "subsets-ii",
1108-
"PassRate": "42%",
1108+
"PassRate": "43%",
11091109
"Difficulty": "Medium",
11101110
"IsAccepted": true,
11111111
"IsPaid": false,
@@ -1465,7 +1465,7 @@
14651465
"ID": 120,
14661466
"Title": "Triangle",
14671467
"TitleSlug": "triangle",
1468-
"PassRate": "39%",
1468+
"PassRate": "40%",
14691469
"Difficulty": "Medium",
14701470
"IsAccepted": true,
14711471
"IsPaid": false,
@@ -2725,7 +2725,7 @@
27252725
"ID": 225,
27262726
"Title": "Implement Stack using Queues",
27272727
"TitleSlug": "implement-stack-using-queues",
2728-
"PassRate": "39%",
2728+
"PassRate": "40%",
27292729
"Difficulty": "Easy",
27302730
"IsAccepted": true,
27312731
"IsPaid": false,
@@ -2953,7 +2953,7 @@
29532953
"ID": 244,
29542954
"Title": "Shortest Word Distance II",
29552955
"TitleSlug": "shortest-word-distance-ii",
2956-
"PassRate": "47%",
2956+
"PassRate": "48%",
29572957
"Difficulty": "Medium",
29582958
"IsAccepted": false,
29592959
"IsPaid": true,
@@ -4765,7 +4765,7 @@
47654765
"ID": 395,
47664766
"Title": "Longest Substring with At Least K Repeating Characters",
47674767
"TitleSlug": "longest-substring-with-at-least-k-repeating-characters",
4768-
"PassRate": "38%",
4768+
"PassRate": "39%",
47694769
"Difficulty": "Medium",
47704770
"IsAccepted": true,
47714771
"IsPaid": false,
@@ -7645,7 +7645,7 @@
76457645
"ID": 635,
76467646
"Title": "Design Log Storage System",
76477647
"TitleSlug": "design-log-storage-system",
7648-
"PassRate": "54%",
7648+
"PassRate": "55%",
76497649
"Difficulty": "Medium",
76507650
"IsAccepted": false,
76517651
"IsPaid": true,
@@ -9265,7 +9265,7 @@
92659265
"ID": 770,
92669266
"Title": "Basic Calculator IV",
92679267
"TitleSlug": "basic-calculator-iv",
9268-
"PassRate": "45%",
9268+
"PassRate": "46%",
92699269
"Difficulty": "Hard",
92709270
"IsAccepted": true,
92719271
"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,
@@ -12121,7 +12121,7 @@
1212112121
"ID": 1008,
1212212122
"Title": "Construct Binary Search Tree from Preorder Traversal",
1212312123
"TitleSlug": "construct-binary-search-tree-from-preorder-traversal",
12124-
"PassRate": "72%",
12124+
"PassRate": "73%",
1212512125
"Difficulty": "Medium",
1212612126
"IsAccepted": true,
1212712127
"IsPaid": false,
@@ -12709,7 +12709,7 @@
1270912709
"ID": 1057,
1271012710
"Title": "Campus Bikes",
1271112711
"TitleSlug": "campus-bikes",
12712-
"PassRate": "59%",
12712+
"PassRate": "60%",
1271312713
"Difficulty": "Medium",
1271412714
"IsAccepted": false,
1271512715
"IsPaid": true,
@@ -12769,7 +12769,7 @@
1276912769
"ID": 1062,
1277012770
"Title": "Longest Repeating Substring",
1277112771
"TitleSlug": "longest-repeating-substring",
12772-
"PassRate": "50%",
12772+
"PassRate": "51%",
1277312773
"Difficulty": "Medium",
1277412774
"IsAccepted": false,
1277512775
"IsPaid": true,
@@ -12781,7 +12781,7 @@
1278112781
"ID": 1063,
1278212782
"Title": "Number of Valid Subarrays",
1278312783
"TitleSlug": "number-of-valid-subarrays",
12784-
"PassRate": "72%",
12784+
"PassRate": "73%",
1278512785
"Difficulty": "Hard",
1278612786
"IsAccepted": false,
1278712787
"IsPaid": true,
@@ -13501,7 +13501,7 @@
1350113501
"ID": 1123,
1350213502
"Title": "Lowest Common Ancestor of Deepest Leaves",
1350313503
"TitleSlug": "lowest-common-ancestor-of-deepest-leaves",
13504-
"PassRate": "65%",
13504+
"PassRate": "64%",
1350513505
"Difficulty": "Medium",
1350613506
"IsAccepted": false,
1350713507
"IsPaid": false,
@@ -13513,7 +13513,7 @@
1351313513
"ID": 1124,
1351413514
"Title": "Longest Well-Performing Interval",
1351513515
"TitleSlug": "longest-well-performing-interval",
13516-
"PassRate": "28%",
13516+
"PassRate": "29%",
1351713517
"Difficulty": "Medium",
1351813518
"IsAccepted": false,
1351913519
"IsPaid": false,
@@ -13561,7 +13561,7 @@
1356113561
"ID": 1128,
1356213562
"Title": "Number of Equivalent Domino Pairs",
1356313563
"TitleSlug": "number-of-equivalent-domino-pairs",
13564-
"PassRate": "39%",
13564+
"PassRate": "40%",
1356513565
"Difficulty": "Easy",
1356613566
"IsAccepted": false,
1356713567
"IsPaid": false,
@@ -13573,7 +13573,7 @@
1357313573
"ID": 1129,
1357413574
"Title": "Shortest Path with Alternating Colors",
1357513575
"TitleSlug": "shortest-path-with-alternating-colors",
13576-
"PassRate": "33%",
13576+
"PassRate": "34%",
1357713577
"Difficulty": "Medium",
1357813578
"IsAccepted": false,
1357913579
"IsPaid": false,
@@ -13585,7 +13585,7 @@
1358513585
"ID": 1130,
1358613586
"Title": "Minimum Cost Tree From Leaf Values",
1358713587
"TitleSlug": "minimum-cost-tree-from-leaf-values",
13588-
"PassRate": "54%",
13588+
"PassRate": "56%",
1358913589
"Difficulty": "Medium",
1359013590
"IsAccepted": false,
1359113591
"IsPaid": false,
@@ -13597,7 +13597,7 @@
1359713597
"ID": 1131,
1359813598
"Title": "Maximum of Absolute Value Expression",
1359913599
"TitleSlug": "maximum-of-absolute-value-expression",
13600-
"PassRate": "47%",
13600+
"PassRate": "48%",
1360113601
"Difficulty": "Medium",
1360213602
"IsAccepted": false,
1360313603
"IsPaid": false,

0 commit comments

Comments
 (0)