一、题目地址
https://www.nowcoder.com/practice/185a87cd29eb42049132aed873273e83?tpId=188&&tqId=38593&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
二、具体代码
function preOrder(root, sum) {
if(!root) {
return 0;
}
sum = sum * 10 + root.val;
if(root.left === null && root.right === null) {
return sum;
}
return preOrder(root.left, sum) + preOrder(root.right, sum);
}
function sumNumbers( root ) {
if(!root) {
return 0;
}
let sum = 0;
return preOrder(root, sum);
}
module.exports = {
sumNumbers : sumNumbers
};