forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path129 Sum Root to Leaf Numbers.js
More file actions
44 lines (38 loc) · 959 Bytes
/
129 Sum Root to Leaf Numbers.js
File metadata and controls
44 lines (38 loc) · 959 Bytes
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
// Leetcode 129
// Language: Javascript
// Problem: https://leetcode.com/problems/sum-root-to-leaf-numbers/
// Author: Chihung Yu
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumNumbers = function(root) {
var total = 0;
if(root === null){
return total;
}
var queue = [];
queue.push(root);
while(queue.length !== 0){
var node = queue.shift();
if(node.left === null && node.right === null){
total += parseInt(node.val);
}
if(node.left){
node.left.val = '' + node.val + node.left.val;
queue.push(node.left);
}
if(node.right){
node.right.val = '' + node.val + node.right.val;
queue.push(node.right);
}
}
return total;
};