Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9840f5e

Browse files
committed
--fix : eslint & code coverage 100%
1 parent b7ec098 commit 9840f5e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

‎src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.js‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
*
44
* Given values of two values n1 and n2 in a Binary Search Tree, find the Lowest Common Ancestor (LCA). You may assume that both the values exist in the tree.
55
*/
6-
6+
77
function lca(node, n1, n2) {
8-
if (node == null)
9-
return null;
8+
if (node == null) return null;
109

1110
// If both n1 and n2 are smaller than root, then LCA lies in left
12-
if (node.value > n1 && node.value > n2)
13-
return lca(node.leftChild, n1, n2);
11+
if (node.value > n1 && node.value > n2) { return lca(node.leftChild, n1, n2); }
1412

1513
// If both n1 and n2 are greater than root, then LCA lies in right
16-
if (node.value < n1 && node.value < n2)
17-
return lca(node.rightChild, n1, n2);
14+
if (node.value < n1 && node.value < n2) { return lca(node.rightChild, n1, n2); }
1815

1916
return node;
2017
}

‎src/_DataStructures_/Trees/BinarySearchTree/lowest-common-ancestor/index.test.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const BinarySearchTree = require('../index');
55
// {"left":{"left":{"data":4},"right":{"left":{"data":10},"right":{"data":14},"data":12},"data":8},"right":{"data":22},"data":20}
66

77
describe('LCA', () => {
8-
9-
let bst = new BinarySearchTree(20);
8+
const bst = new BinarySearchTree(20);
109
bst.add(22);
1110
bst.add(8);
1211
bst.add(12);
@@ -17,12 +16,19 @@ describe('LCA', () => {
1716
it('Should return 12', () => {
1817
expect(lca(bst.root, 10, 14).value).toEqual(12);
1918
});
20-
19+
2120
it('Should return 8', () => {
2221
expect(lca(bst.root, 14, 8).value).toEqual(8);
2322
});
24-
23+
2524
it('Should return 20', () => {
2625
expect(lca(bst.root, 10, 22).value).toEqual(20);
2726
});
27+
28+
const bst2 = new BinarySearchTree(6);
29+
bst2.remove(6);
30+
31+
it('Should return Null', () => {
32+
expect(lca(bst2.root, 10, 22)).toEqual(null);
33+
});
2834
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /