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

324
Visualizações
How to determine if a binary tree is balanced or not using recursion?

I am trying to return true if the tree is balanced and false if not, I came up with this recursive solution below but I am not correct the correct boolean. I feel that it makes sense to compare the highest height of the tree vs the lower height? Not sure where I am going wrong

function tree (rootNode) {
  // Your code here
  if (!rootNode) return 0;
  if (!rootNode.left && !rootNode.right) return 0;
  let minHeigth = 1 + Math.min(tree(rootNode.left), tree(rootNode.right))
  let maxHeigth = 1 + Math.max(tree(rootNode.left), tree(rootNode.right))
  if(maxHeigth - minHeigth <= 1){
    return true
  }else{
    return false
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

My recursive algorithm will be like this.

function isBalanced(rootNode){
  if(rootNode == null){
     return true;
   }
  if(checkHeight(rootNode) === -1){ 
    return false; 
  } else{
    return isBalanced(root.left) && isBalanced(root.right);  
  }
}

and the helper method checkHeight which will check height will be

function checkHeight(rootNode){
  if(root ==null){
    return 0;
  }
  let leftHeight=checkHeight(root.left);
  let rightHeight=checkHeight(root.right);
  if(Math.abs(leftHeight - rightHeight) > 1){
    return -1;
  }
  else{
    return Math.max(leftHeight,rightHeight) +1;
  }
}

NOTE: This Algo will have the time complexity of O(N)

about 4 years ago · Juan Pablo Isaza Relatório

0

The main issue is that your function is mixing two things:

  • Returning the height (number)
  • Returning whether it is balanced (boolean)

If the ultimate purpose is to return a boolean, then you need a different function for getting the height (a number).

Secondly, in determining the height the first two lines of your code show an inconsistency:

  • for a null (empty) tree it is determined that its height is 0
  • for a node without children, it is also determined that its height is 0

Yet these two trees have a different height. If a single node (root) is considered to have height 0, then an empty tree has height -1. This is in line with Wikipedia:

The height of a node is the length of the longest downward path to a leaf from that node. The height of the root is the height of the tree. The depth of a node is the length of the path to its root (i.e., its root path). This is commonly needed in the manipulation of the various self-balancing trees, AVL Trees in particular. The root node has depth zero, leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has depth and height zero. Conventionally, an empty tree (tree with no nodes, if such are allowed) has height −1.

about 4 years ago · Juan Pablo Isaza 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