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

260
Vistas
Node is not Empty then what exactly we return in JavaScript code

Here is soln for Binary Tree Right Side View where trying to solve below problem

enter image description here

Input: root = [1,2,3,null,5,null,4] Output: [1,3,4]

Input: root = [1,null,3] Output: [1,3]

Code with comments provided below

            var rightSideView = function(root) {
              const levels = [];
            //DFS solutions often allow us to find a concise, recursive solution, and while they're not always the first thought when it comes to tree traversal problems where the level is important, in this case we don't need the level as a whole, we just need one end of each level. 
              dfs(root, levels)
              const res = [];
              for(let l of levels){
                res.push(l.pop())
              }
              return res
            };

            function dfs(root, levels, level = 0){
             // Base Case
              if(!root) return;
              if(!levels[level]){
                levels[level] = [];
              }
              levels[level].push(root.val);
            // Recur for left subtree then right subtree 

              dfs(root.left, levels, level + 1)
              dfs(root.right, levels, level + 1)
            }

Questions are :-

  1. The line if(!root) return; What does it return exactly
  2. Does the comments provided in code are right?

Your response is much appreciated

Regards

Carolyn

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