-
Notifications
You must be signed in to change notification settings - Fork 0
/
129.js
44 lines (40 loc) · 1.06 KB
/
129.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumNumbers = function (root) {
let str = ''
let res = 0
const dfs = root => {
if (root === null) return
if (root.left === null && root.right === null) {
str += root.val
res += Number(str)
str = str.slice(0, -1)
return
}
str += root.val
root.left ? dfs(root.left) : ''
root.right ? dfs(root.right) : ''
/* dfs(root.left)
dfs(root.right) */
str = str.slice(0, -1)
return
}
dfs(root)
return res
};
/*
2021/9/14
85 97
如果2627两行换成2829的话就是69 6,差距这么大的吗,我不理解
我用了字符串进行操作,也可以多给dfs传一个数字作为参数进行通信,能减小操作量
*/