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

257
Visualizações
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 à 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