-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_008.js
65 lines (60 loc) Β· 1.48 KB
/
problem_008.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
62
63
64
65
class Node {
constructor(value, left=null, right=null) {
this.left = left;
this.right = right;
this.value = value;
}
}
const univalTree = (root) => {
var univalCount = 0;
const processSubtrees = (node, value) => {
if(node.right || node.left) {
// Check right subtree
if(node.right) {
if(node.right.value === value) {
if(processSubtrees(node.right, value)) {
univalCount++;
}
} else {
// Recurse right subtree
return 1 + processSubtrees(node.right, value);
}
}
// Check left subtree
if(node.left) {
if(node.left.value === value) {
if(processSubtrees(node.left, value)) {
univalCount++;
}
} else {
// Recurse left subtree
return 1 + processSubtrees(node.left, value);
}
}
} else {
univalCount++;
return true;
}
}
processSubtrees(root, root.value);
return univalCount;
}
// Creates tree example
node_a = new Node(0);
node_b = new Node(1);
node_c = new Node(0);
node_d = new Node(1);
node_e = new Node(0);
node_f = new Node(1);
node_g = new Node(1);
node_a.left = node_b;
node_a.right = node_c;
node_c.left = node_d;
node_c.right = node_e;
node_d.left = node_f;
node_d.right = node_g;
console.log(univalTree(node_a)); // 5
console.log(univalTree(node_b)); // 1
console.log(univalTree(node_c)); // 3
console.log(univalTree(node_g)); // 1
console.log(univalTree(node_d)); // 4