Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Problemas para encontrar la altura de mi primer árbol NO binario en JS

Aquí está la función constructora, así como el método 'agregar' ...

 function Tree(value) { this.value = value; this.children = []; } Tree.prototype.add = function(...value) { if (Array.isArray(value)) { for (let i = 0; i < value.length; i++) { let node = new Tree; node.value = value[i]; this.children[this.children.length] = node; } return; } let node = new Tree; node.value = value; this.children[this.children.length] = node; };

Aquí está el método de 'altura' en el que estoy trabajando ...

 Tree.prototype.height = function() { let height = 0; if (this.children != null) { if (this.children.length == 0) { return height; } else { for (let i = 0; i < this.children.length; i++) { height = Math.max(height, this.height(this.children[i])); } return depth + 1; } return height; } };

No estoy seguro de por qué obtengo "RangeError: se excedió el tamaño máximo de la pila de llamadas" porque el método debería devolver la altura si la longitud de la matriz secundaria es 0.

Aquí están los casos de prueba que estoy usando...

 tree = new Tree(); tree.add(5); tree.add(8); var son = tree.children[0]; son.add(1); son.add(4); var daughter = tree.children[0]; daughter.add(10); var grandson = son.children[1]; grandson.add(3); grandson.height() //should return 1 tree.height() //should return

Cualquier consejo sobre cómo arreglar mi bucle infinito será muy apreciado. ¡Gracias por tu tiempo!

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda