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

Commit a5ed33b

Browse files
committed
1022 accepted.
1 parent 2a8f304 commit a5ed33b

File tree

5 files changed

+132
-27
lines changed

5 files changed

+132
-27
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# [1022. Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)
2+
3+
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
4+
5+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
6+
7+
Return the sum of these numbers.
8+
9+
Example 1:
10+
11+
![1](1.png)
12+
13+
```text
14+
Input: [1,0,1,0,1,0,1]
15+
Output: 22
16+
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
17+
```
18+
19+
Note:
20+
21+
1. The number of nodes in the tree is between 1 and 1000.
22+
1. node.val is 0 or 1.
23+
1. The answer will not exceed 2^31 - 1.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem1022
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// TreeNode is pre-defined...
6+
// type TreeNode struct {
7+
// Val int
8+
// Left *TreeNode
9+
// Right *TreeNode
10+
// }
11+
type TreeNode = kit.TreeNode
12+
13+
func sumRootToLeaf(root *TreeNode) int {
14+
res := 0
15+
dfs(root, 0, &res)
16+
return res
17+
}
18+
19+
func dfs(node *TreeNode, num int, res *int) {
20+
num = num*2 + node.Val
21+
if node.Left == nil && node.Right == nil {
22+
*res += num
23+
return
24+
}
25+
if node.Left != nil {
26+
dfs(node.Left, num, res)
27+
}
28+
if node.Right != nil {
29+
dfs(node.Right, num, res)
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problem1022
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// tcs is testcase slice
12+
var tcs = []struct {
13+
root []int
14+
ans int
15+
}{
16+
17+
{
18+
[]int{1, kit.NULL, 0},
19+
2,
20+
},
21+
22+
{
23+
[]int{1, 1},
24+
3,
25+
},
26+
27+
{
28+
[]int{1, 0, 1, 0, 1, 0, 1},
29+
22,
30+
},
31+
32+
// 可以有多个 testcase
33+
}
34+
35+
func Test_sumRootToLeaf(t *testing.T) {
36+
ast := assert.New(t)
37+
38+
for _, tc := range tcs {
39+
root := kit.Ints2TreeNode(tc.root)
40+
ast.Equal(tc.ans, sumRootToLeaf(root), "输入:%v", tc)
41+
}
42+
}
43+
44+
func Benchmark_sumRootToLeaf(b *testing.B) {
45+
for i := 0; i < b.N; i++ {
46+
for _, tc := range tcs {
47+
root := kit.Ints2TreeNode(tc.root)
48+
sumRootToLeaf(root)
49+
}
50+
}
51+
}

leetcode.json

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 669,
4-
"Updated": "2019-06-25T15:36:11.152308221+08:00",
4+
"Updated": "2019-06-26T19:47:01.273910249+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 237,
@@ -1381,7 +1381,7 @@
13811381
"ID": 113,
13821382
"Title": "Path Sum II",
13831383
"TitleSlug": "path-sum-ii",
1384-
"PassRate": "40%",
1384+
"PassRate": "41%",
13851385
"Difficulty": "Medium",
13861386
"IsAccepted": true,
13871387
"IsPaid": false,
@@ -2101,7 +2101,7 @@
21012101
"ID": 173,
21022102
"Title": "Binary Search Tree Iterator",
21032103
"TitleSlug": "binary-search-tree-iterator",
2104-
"PassRate": "48%",
2104+
"PassRate": "49%",
21052105
"Difficulty": "Medium",
21062106
"IsAccepted": true,
21072107
"IsPaid": false,
@@ -3577,7 +3577,7 @@
35773577
"ID": 296,
35783578
"Title": "Best Meeting Point",
35793579
"TitleSlug": "best-meeting-point",
3580-
"PassRate": "54%",
3580+
"PassRate": "55%",
35813581
"Difficulty": "Hard",
35823582
"IsAccepted": false,
35833583
"IsPaid": true,
@@ -4741,7 +4741,7 @@
47414741
"ID": 393,
47424742
"Title": "UTF-8 Validation",
47434743
"TitleSlug": "utf-8-validation",
4744-
"PassRate": "35%",
4744+
"PassRate": "36%",
47454745
"Difficulty": "Medium",
47464746
"IsAccepted": true,
47474747
"IsPaid": false,
@@ -5197,7 +5197,7 @@
51975197
"ID": 431,
51985198
"Title": "Encode N-ary Tree to Binary Tree",
51995199
"TitleSlug": "encode-n-ary-tree-to-binary-tree",
5200-
"PassRate": "63%",
5200+
"PassRate": "64%",
52015201
"Difficulty": "Hard",
52025202
"IsAccepted": false,
52035203
"IsPaid": true,
@@ -5389,7 +5389,7 @@
53895389
"ID": 447,
53905390
"Title": "Number of Boomerangs",
53915391
"TitleSlug": "number-of-boomerangs",
5392-
"PassRate": "49%",
5392+
"PassRate": "50%",
53935393
"Difficulty": "Easy",
53945394
"IsAccepted": true,
53955395
"IsPaid": false,
@@ -6685,7 +6685,7 @@
66856685
"ID": 555,
66866686
"Title": "Split Concatenated Strings",
66876687
"TitleSlug": "split-concatenated-strings",
6688-
"PassRate": "39%",
6688+
"PassRate": "40%",
66896689
"Difficulty": "Medium",
66906690
"IsAccepted": false,
66916691
"IsPaid": true,
@@ -7885,7 +7885,7 @@
78857885
"ID": 655,
78867886
"Title": "Print Binary Tree",
78877887
"TitleSlug": "print-binary-tree",
7888-
"PassRate": "52%",
7888+
"PassRate": "51%",
78897889
"Difficulty": "Medium",
78907890
"IsAccepted": true,
78917891
"IsPaid": false,
@@ -8173,7 +8173,7 @@
81738173
"ID": 679,
81748174
"Title": "24 Game",
81758175
"TitleSlug": "24-game",
8176-
"PassRate": "42%",
8176+
"PassRate": "43%",
81778177
"Difficulty": "Hard",
81788178
"IsAccepted": true,
81798179
"IsPaid": false,
@@ -8473,7 +8473,7 @@
84738473
"ID": 704,
84748474
"Title": "Binary Search",
84758475
"TitleSlug": "binary-search",
8476-
"PassRate": "47%",
8476+
"PassRate": "48%",
84778477
"Difficulty": "Easy",
84788478
"IsAccepted": true,
84798479
"IsPaid": false,
@@ -10081,7 +10081,7 @@
1008110081
"ID": 838,
1008210082
"Title": "Push Dominoes",
1008310083
"TitleSlug": "push-dominoes",
10084-
"PassRate": "43%",
10084+
"PassRate": "44%",
1008510085
"Difficulty": "Medium",
1008610086
"IsAccepted": true,
1008710087
"IsPaid": false,
@@ -10225,7 +10225,7 @@
1022510225
"ID": 850,
1022610226
"Title": "Rectangle Area II",
1022710227
"TitleSlug": "rectangle-area-ii",
10228-
"PassRate": "44%",
10228+
"PassRate": "45%",
1022910229
"Difficulty": "Hard",
1023010230
"IsAccepted": true,
1023110231
"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,
@@ -10753,7 +10753,7 @@
1075310753
"ID": 894,
1075410754
"Title": "All Possible Full Binary Trees",
1075510755
"TitleSlug": "all-possible-full-binary-trees",
10756-
"PassRate": "71%",
10756+
"PassRate": "70%",
1075710757
"Difficulty": "Medium",
1075810758
"IsAccepted": true,
1075910759
"IsPaid": false,
@@ -11245,7 +11245,7 @@
1124511245
"ID": 935,
1124611246
"Title": "Knight Dialer",
1124711247
"TitleSlug": "knight-dialer",
11248-
"PassRate": "41%",
11248+
"PassRate": "40%",
1124911249
"Difficulty": "Medium",
1125011250
"IsAccepted": true,
1125111251
"IsPaid": false,
@@ -11785,7 +11785,7 @@
1178511785
"ID": 980,
1178611786
"Title": "Unique Paths III",
1178711787
"TitleSlug": "unique-paths-iii",
11788-
"PassRate": "70%",
11788+
"PassRate": "71%",
1178911789
"Difficulty": "Hard",
1179011790
"IsAccepted": true,
1179111791
"IsPaid": false,
@@ -11809,7 +11809,7 @@
1180911809
"ID": 982,
1181011810
"Title": "Triples with Bitwise AND Equal To Zero",
1181111811
"TitleSlug": "triples-with-bitwise-and-equal-to-zero",
11812-
"PassRate": "53%",
11812+
"PassRate": "54%",
1181311813
"Difficulty": "Hard",
1181411814
"IsAccepted": true,
1181511815
"IsPaid": false,
@@ -12025,7 +12025,7 @@
1202512025
"ID": 1000,
1202612026
"Title": "Minimum Cost to Merge Stones",
1202712027
"TitleSlug": "minimum-cost-to-merge-stones",
12028-
"PassRate": "31%",
12028+
"PassRate": "32%",
1202912029
"Difficulty": "Hard",
1203012030
"IsAccepted": true,
1203112031
"IsPaid": false,
@@ -12505,7 +12505,7 @@
1250512505
"ID": 1040,
1250612506
"Title": "Moving Stones Until Consecutive II",
1250712507
"TitleSlug": "moving-stones-until-consecutive-ii",
12508-
"PassRate": "46%",
12508+
"PassRate": "45%",
1250912509
"Difficulty": "Medium",
1251012510
"IsAccepted": true,
1251112511
"IsPaid": false,
@@ -12733,7 +12733,7 @@
1273312733
"ID": 1059,
1273412734
"Title": "All Paths from Source Lead to Destination",
1273512735
"TitleSlug": "all-paths-from-source-lead-to-destination",
12736-
"PassRate": "46%",
12736+
"PassRate": "45%",
1273712737
"Difficulty": "Medium",
1273812738
"IsAccepted": false,
1273912739
"IsPaid": true,
@@ -12817,7 +12817,7 @@
1281712817
"ID": 1066,
1281812818
"Title": "Campus Bikes II",
1281912819
"TitleSlug": "campus-bikes-ii",
12820-
"PassRate": "42%",
12820+
"PassRate": "43%",
1282112821
"Difficulty": "Medium",
1282212822
"IsAccepted": false,
1282312823
"IsPaid": true,
@@ -12829,7 +12829,7 @@
1282912829
"ID": 1067,
1283012830
"Title": "Digit Count in Range",
1283112831
"TitleSlug": "digit-count-in-range",
12832-
"PassRate": "34%",
12832+
"PassRate": "33%",
1283312833
"Difficulty": "Hard",
1283412834
"IsAccepted": false,
1283512835
"IsPaid": true,
@@ -12973,7 +12973,7 @@
1297312973
"ID": 1079,
1297412974
"Title": "Letter Tile Possibilities",
1297512975
"TitleSlug": "letter-tile-possibilities",
12976-
"PassRate": "77%",
12976+
"PassRate": "76%",
1297712977
"Difficulty": "Medium",
1297812978
"IsAccepted": false,
1297912979
"IsPaid": false,
@@ -13057,7 +13057,7 @@
1305713057
"ID": 1086,
1305813058
"Title": "High Five",
1305913059
"TitleSlug": "high-five",
13060-
"PassRate": "76%",
13060+
"PassRate": "77%",
1306113061
"Difficulty": "Easy",
1306213062
"IsAccepted": false,
1306313063
"IsPaid": true,
@@ -13081,7 +13081,7 @@
1308113081
"ID": 1088,
1308213082
"Title": "Confusing Number II",
1308313083
"TitleSlug": "confusing-number-ii",
13084-
"PassRate": "33%",
13084+
"PassRate": "34%",
1308513085
"Difficulty": "Hard",
1308613086
"IsAccepted": false,
1308713087
"IsPaid": true,
@@ -13141,7 +13141,7 @@
1314113141
"ID": 1093,
1314213142
"Title": "Statistics from a Large Sample",
1314313143
"TitleSlug": "statistics-from-a-large-sample",
13144-
"PassRate": "39%",
13144+
"PassRate": "40%",
1314513145
"Difficulty": "Medium",
1314613146
"IsAccepted": false,
1314713147
"IsPaid": false,

0 commit comments

Comments
 (0)