8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 59f46c8 commit bc70deeCopy full SHA for bc70dee
Binary-Tree/1022/solution1.js
@@ -5,18 +5,19 @@
5
*/
6
const sumRootToLeaf = root => {
7
let sum = 0;
8
- const help = (root, path) => {
+ const dfs = (root, path) => {
9
if (!root) {
10
return;
11
}
12
- const newPath = path + '' + root.val;
+ path += String(root.val);
13
if (!root.left && !root.right) {
14
- sum += Number.parseInt(newPath, 2);
+ sum += Number.parseInt(path, 2);
15
+ return;
16
- help(root.left, newPath);
17
- help(root.right, newPath);
18
- }
19
20
- help(root, '');
+ dfs(root.left, path);
+ dfs(root.right, path);
+ }
21
+ dfs(root, '');
22
return sum;
23
0 commit comments