Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!