Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Найти сумму значений всех узлов дерева
- */
- function getSum(obj) {
- var arr = [obj], // очередь
- sum = 0,
- current;
- while(arr.length > 0) { // пока очередь не пустая
- current = arr.shift(); // удаляем последний элемент очереди и присваем его текущей ноде
- sum += current.valueNode;
- if (current.next) { // если в ноде есть дети записываем их в очередь
- current.next.forEach(el => {
- arr.push(el);
- })
- }
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement