I have an array.
const arr1 = [3, [ 8, [ 5, 4, null], 11], [ 7, [ 1, 0, null], null]]
I want to write a function, which should check if given value is in tree or not.
Here is my function.
function valueInTree(tree, val) {
if(tree[0] === val || tree[1] === val || tree [2] === val){
return true;
}
if (Array.isArray(tree[1])){
return valueInTree(tree[1],val)
}
if (Array.isArray(tree[2])){
return valueInTree(tree[2],val);
}
return false;
}
console.log(valueInTree(arr1, 72));
Below is a visual of the given array.
// 3
// / \
// 8 7
// /\ /\
// 5 11 1 N
// /\ / \
// 4 72 0 N
So, my question. As you can see, my function cannot check the right side of the tree. For example, it can find numbers 3, 8, 5, and 4. But when I try to find 7 or 11 it returns false.