Skip to content

Commit 2f89037

Browse files
authored
Create 1022-sum-of-root-to-leaf-binary-numbers.js
1 parent 869ffa9 commit 2f89037

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const sumRootToLeaf = function(root) {
13+
if(root == null) return 0
14+
const res = []
15+
dfs(root, 0, res)
16+
const mod = Math.pow(10, 9) + 7
17+
return res.reduce((ac, el) => (ac + el) % mod ,0)
18+
};
19+
20+
function dfs(node, val, res) {
21+
const mod = Math.pow(10, 9) + 7
22+
if(node == null) return
23+
val = (val * 2 + node.val) % mod
24+
if(node.left === null && node.right === null) {
25+
res.push(val)
26+
}
27+
dfs(node.left, val, res)
28+
dfs(node.right, val, res)
29+
}

0 commit comments

Comments
 (0)