Unfortunately, your question is based on a faulty premise. The code to be reviewed consists of a makeBst()
function with one test case. That test case produces incorrect output:
class BinaryTreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function makeBst(arr){
if(!arr || arr.length <= 1){
return arr;
}
let top = arr[Math.floor(arr.length / 2)];
let node = new BinaryTreeNode(top);
let rightArr = arr.splice(Math.floor(arr.length /2), arr.length);
let leftArr = arr.splice(0, Math.floor(arr.length / 2));
node.right = makeBst(rightArr);
node.left = makeBst(leftArr);
return node;
}
const arr = [1, 2, 3, 4, 5, 6, 7];
console.log(makeBst(arr));
The output:
4
/ \
1 6
/ \
4 7
\
7
So, applying some principles for evaluating whether the question is off-topic, I would conclude that the author should have been fully aware that the code is broken. I would consider the claim that "The function returns the correct output, and all known edge cases have been accounted for" to be a lie.
It would be unfair to allow the author to get the blatantly broken code reviewed (and possibly get a free bugfix out of the review) just by making a false claim that it works. You can't work around the "to the best of your knowledge, does the code work?" requirement by lying. This question should therefore be closed as off-topic.
I realize that you had invested time to look at the code before realizing that you have been misled. Unfortunately, that happens sometimes. The way to discourage such unfair behaviour is to boycott these questions, not by answering them.