Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 39fe30e

Browse files
committed
Merge pull request #76 from g-palmer/checkBST
binary search tree check
2 parents 69376b6 + 61cc687 commit 39fe30e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// depth first traversal checking sort order of each node's value against previous
2+
3+
var isBinarySeachTree = function(bst) {
4+
// reference to previous node value
5+
var prev;
6+
7+
// return IIFE of recursive bst traversal
8+
return (function isBST(bst) {
9+
10+
// if left, check left and break if necessary
11+
if ( bst.left && !isBST(bst.left) ) { return false; }
12+
13+
// break if current node value is less than previous
14+
if ( bst.value < prev ) { return false; }
15+
16+
// set previous to current node value
17+
prev = bst.value;
18+
19+
// if right, check right and break if necessary
20+
if (bst.right && !isBST(bst.right) ) { return false; }
21+
22+
// passes test
23+
return true;
24+
25+
})(bst);
26+
};

0 commit comments

Comments
 (0)