8000 feat(1022): 递归&进制 · descire/LeetCode@bc70dee · GitHub
[go: up one dir, main page]

Skip to content

Commit bc70dee

Browse files
committed
feat(1022): 递归&进制
1 parent 59f46c8 commit bc70dee

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Binary-Tree/1022/solution1.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
*/
66
const sumRootToLeaf = root => {
77
let sum = 0;
8-
const help = (root, path) => {
8+
const dfs = (root, path) => {
99
if (!root) {
1010
return;
1111
}
12-
const newPath = path + '' + root.val;
12+
path += String(root.val);
1313
if (!root.left && !root.right) {
14-
sum += Number.parseInt(newPath, 2);
14+
sum += Number.parseInt(path, 2);
15+
return;
1516
}
16-
help(root.left, newPath);
17-
help(root.right, newPath);
18-
}
1917

20-
help(root, '');
18+
dfs(root.left, path);
19+
dfs(root.right, path);
20+
}
21+
dfs(root, '');
2122
return sum;
2223
}

0 commit comments

Comments
 (0)
0