-
Notifications
You must be signed in to change notification settings - Fork 6
/
findSumInTree.js
61 lines (51 loc) · 1.25 KB
/
findSumInTree.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// You are given a binary tree in which each node contains a value. Design an algorithm
// to print all paths which sum up to that value. Note that it can be any path in the tree
// - it does not have to start at the root.
const BST = require("./DataStructures/BST/BinarySearchTree");
let root = new BST(); /*?*/
root.add(4);
root.add(3);
root.add(1);
root.add(10);
root.add(2);
root.add(6);
root.add(7);
root.add(5);
function traverse(root, desireSum, paths = []) {
findSumInTree(root, desireSum, [], 0, paths);
if (root.left) {
traverse(root.left, desireSum, paths);
}
if (root.right) {
traverse(root.right, desireSum, paths);
}
return paths;
}
function findSumInTree(root, desireSum, tempPaths, currentSum, paths) {
if (currentSum + root.value === desireSum) {
tempPaths.push(root.value);
return paths.push(tempPaths);
}
if (currentSum + root.value > desireSum) {
return;
}
if (root.left) {
findSumInTree(
root.left,
desireSum,
[root.value, ...tempPaths],
currentSum + root.value,
paths
);
}
if (root.right) {
findSumInTree(
root.right,
desireSum,
[root.value, ...tempPaths],
currentSum + root.value,
paths
);
}
}
console.log(traverse(root, 3));