Skip to content

Commit fee9a97

Browse files
committed
Added medium/binary_tree_lca
1 parent 3ef2d96 commit fee9a97

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

medium/binary_tree_lca.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Using the JavaScript language, have the function binaryTreeLCA(strArr) take
3+
* the array of strings stored in strArr, which will contain 3 elements: the
4+
* first element will be a binary tree with all unique values in a format
5+
* similar to how a binary heap is implemented with NULL nodes at any level
6+
* represented with a #, the second and third elements will be two different
7+
* values, and your goal is to find the lowest common ancestor of these two
8+
* values. For example: if strArr is ["[12, 5, 9, 6, 2, 0, 8, #, #, 7, 4, #, #,
9+
* #, #]", "6", "4"] then this tree looks like the following:
10+
*
11+
* [Image of Example Tree](https://i.imgur.com/YrPRxyx.png)
12+
*
13+
* For the input above, your program should return 5 because that is the value
14+
* of the node that is the LCA of the two nodes with values 6 and 4. You can
15+
* assume the two nodes you are searching for in the tree will exist somewhere
16+
* in the tree.
17+
*
18+
* https://www.coderbyte.com/results/bhanson:Binary%20Tree%20LCA:JavaScript
19+
*
20+
* @param {array} strArr
21+
* @return {number}
22+
*/
23+
function binaryTreeLca(strArr) {
24+
// Coderbyte.com calls this "Binary Tree LCA", but really this should be
25+
// called "Binary Max Heap LCA"
26+
// https://en.wikipedia.org/wiki/Binary_heap
27+
28+
const heapNodes = strArr.shift().match(/[\d#]+/g);
29+
const searchNodes = strArr.map(Number);
30+
31+
const nodeAPath = path(heapNodes, searchNodes[0]);
32+
const nodeBPath = path(heapNodes, searchNodes[1]);
33+
34+
// Find LCA
35+
for (let i = 0; i < nodeAPath.length; i++) {
36+
if (nodeBPath.includes(nodeAPath[i])) {
37+
return Number(nodeAPath[i]);
38+
}
39+
}
40+
return -1;
41+
}
42+
43+
// returns array of path to key, or empty array if doesn't exist
44+
// https://en.wikipedia.org/wiki/Binary_heap#Heap_implementation
45+
function path(arr, key) {
46+
const pathArr = [];
47+
let keyIndex = arr.indexOf(String(key));
48+
if (keyIndex === -1) {
49+
return [];
50+
}
51+
52+
while (keyIndex !== 0) {
53+
pathArr.push(arr[keyIndex]);
54+
const parentIndex = Math.floor((keyIndex - 1) / 2);
55+
keyIndex = parentIndex;
56+
}
57+
pathArr.push(arr[keyIndex]); // keyIndex === 0
58+
return pathArr;
59+
}
60+
61+
module.exports = binaryTreeLca;

medium/binary_tree_lca.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const binaryTreeLca = require('./binary_tree_lca');
2+
3+
describe('binaryTreeLca()', () => {
4+
test('correctly returns least common ancestor', () => {
5+
expect(
6+
binaryTreeLca([
7+
'[12, 5, 9, 6, 2, 0, 8, #, #, 7, 4, #, #, #, #]',
8+
'6',
9+
'4'
10+
])
11+
).toBe(5);
12+
13+
expect(binaryTreeLca(['[5, 2, 6, 1, #, 8, #]', '2', '6'])).toBe(5);
14+
15+
expect(
16+
binaryTreeLca([
17+
'[5, 2, 6, 1, #, 8, 12, #, #, #, #, #, #, 3, #]',
18+
'3',
19+
'12'
20+
])
21+
).toBe(12);
22+
});
23+
24+
test('passes Coderbyte.com tests', () => {
25+
expect(
26+
binaryTreeLca([
27+
'[12, 5, 9, 6, 2, 0, 8, #, #, 7, 4, #, #, #, #]',
28+
'6',
29+
'4'
30+
])
31+
).toBe(5);
32+
33+
expect(binaryTreeLca(['[5, 2, 6, 1, #, 8, #]', '2', '6'])).toBe(5);
34+
35+
expect(
36+
binaryTreeLca([
37+
'[5, 2, 6, 1, #, 8, 12, #, #, #, #, #, #, 3, #]',
38+
'3',
39+
'12'
40+
])
41+
).toBe(12);
42+
43+
expect(
44+
binaryTreeLca([
45+
'[5, 2, 6, 1, #, 8, 12, #, #, #, #, #, #, 3, #]',
46+
'3',
47+
'8'
48+
])
49+
).toBe(6);
50+
51+
expect(
52+
binaryTreeLca([
53+
'[5, 2, 6, 1, #, 8, 12, #, #, #, #, #, #, 3, #]',
54+
'1',
55+
'8'
56+
])
57+
).toBe(5);
58+
59+
expect(binaryTreeLca(['[1, 2, 3]', '2', '3'])).toBe(1);
60+
61+
expect(binaryTreeLca(['[1, 2, 3]', '1', '3'])).toBe(1);
62+
63+
expect(
64+
binaryTreeLca([
65+
'[12, 5, 9, 6, 2, 0, 8, #, #, 7, 4, #, #, #, #]',
66+
'6',
67+
'0'
68+
])
69+
).toBe(12);
70+
71+
expect(
72+
binaryTreeLca([
73+
'[12, 5, 9, 6, 2, 0, 8, #, #, 15, 22, #, #, #, #]',
74+
'15',
75+
'22'
76+
])
77+
).toBe(2);
78+
79+
expect(binaryTreeLca(['[5, 8, 10, 12, #, #, 18]', '12', '18'])).toBe(5);
80+
});
81+
});

0 commit comments

Comments
 (0)