-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree-lowest-common-ancestor.js
131 lines (104 loc) · 3.49 KB
/
binary-search-tree-lowest-common-ancestor.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function lessThanOrEqual(nodeA, nodeB) {
return nodeA.data <= nodeB.data;
}
function greaterThan(nodeA, nodeB) {
return nodeA.data > nodeB.data;
}
class Tree {
constructor(rootNode) {
this.root = rootNode;
}
insert(value) {
const node = new Node(value);
const queue = [this.root];
while(queue.length > 0) {
const prospectiveParent = queue.shift();
if (lessThanOrEqual(node, prospectiveParent)) {
if(prospectiveParent.left === null) {
prospectiveParent.left = node;
break;
} else {
queue.push(prospectiveParent.left);
}
} else if (greaterThan(node, prospectiveParent)) {
if(prospectiveParent.right === null) {
prospectiveParent.right = node;
break;
} else {
queue.push(prospectiveParent.right);
}
}
}
}
}
class Node {
constructor(data, left = null, right = null) {
this.data = data;
this.left = left;
this.right = right;
}
}
function findPath(vertex, target, path) {
if (vertex === null) {
return null;
} else if (vertex.data === target) {
return path.concat(vertex);
} else {
const leftPath = findPath(vertex.left, target, path.concat(vertex));
const rightPath = findPath(vertex.right, target, path.concat(vertex));
return leftPath ? leftPath : rightPath;
}
}
function intersection(pathA, pathB) {
const int = [];
while(pathA.length > 0 && pathB.length > 0) {
const vertexA = pathA.shift();
const vertexB = pathB.shift();
if (vertexA.data === vertexB.data) {
int.push(vertexA);
}
}
return int;
}
function diffPaths(paths) {
const diff = [paths[0]];
for (let i = 1; i < paths.length; i++) {
const previousPath = diff[i - 1];
const currentPath = paths[i];
diff.push(intersection(previousPath, currentPath));
}
return diff;
}
function lowestCommonAncestor(tree, targets) {
const paths = [];
for (const target of targets) {
paths.push(findPath(tree.root, target, []));
}
const diffs = diffPaths(paths);
const finalDiff = diffs[diffs.length - 1];
return finalDiff[finalDiff.length - 1];
}
function processData(input) {
//Enter your code here
const inputs = input.split("\n");
const args = inputs.filter((input) => !!input);
for (let i = 0; i < args.length; i += 3) {
const numNodes = parseInt(args[i]);
const nodeDatas = (args[i + 1].split(" ")).map((val) => parseInt(val));
const targets = (args[i + 2].split(" ")).map((val) => parseInt(val));
const tree = new Tree(new Node(nodeDatas.shift()));
for (const value of nodeDatas) {
tree.insert(value);
}
process.stdout.write(`${lowestCommonAncestor(tree, targets).data}`);
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});