-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
31 lines (30 loc) · 924 Bytes
/
soln.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int pathSum(vector<int>& nums) {
if (nums.empty()) return 0;
vector<vector<int>> tree(10, vector<int>(20, -1));
for(int num : nums) {
int V = num % 10;
num /= 10;
int P = num % 10;
num /= 10;
int D = num % 10;
tree[D][P] = V;
}
int ans = 0;
dfs(tree, 1, 1, 0, ans);
return ans;
}
void dfs(vector<vector<int>> & tree, int D, int P, int path, int & ans) {
if (tree[D + 1][P * 2 - 1] == -1 && tree[D + 1][P * 2] == -1) {
ans += path + tree[D][P];
} else {
if (tree[D + 1][P * 2 - 1] != -1) {
dfs(tree, D + 1, P * 2 - 1, path + tree[D][P], ans);
}
if (tree[D + 1][P * 2] != -1) {
dfs(tree, D + 1, P * 2, path + tree[D][P], ans);
}
}
}
};