Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Finding a value in a Binary Search Tree

I am making the code of a function to find a value passed by parameter in a BinarySearchTree but it is not working for me and I don't know why.if the price is inside the tree I have to return true, if the price passed by parameter is less than or equal to 0 I have to return "error" and if it is not found I must return false this is my code.

BinarySearchTree.prototype.searchPrice = function (price) {

    if (price <= 0) return "Error"  
    
    if(this.value === price) return true;

    if(this.value < price) return this.right.searchPrice(price);

    if(this.value > price) return this.left.searchPrice(price);

    if(this.value === null) return false;
};
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The issue might be that you have null check for this.value at the end which causes the if(this.value < price) always to be true when this.value is null

Try to move the line if(this.value === null) return false; at the top and see if it works

UPDATE: Also you need to add null checks for the this.left and this.right

BinarySearchTree.prototype.searchPrice = function (price) {

    if (price <= 0) return "Error"  

// move this line at the top like this
    if(this.value === null) return false;
    
    if(this.value === price) return true;

    if(this.value < price) {
        if(!this.right) {
            return false;
        }
        return this.right.searchPrice(price);
    }

    if(this.value > price){
        if(!this.left) {
            return false;
        }
        return this.left.searchPrice(price);
    }

};
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda