Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Tree Level Order Traversal using Javascript

This is a leetcode question.

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example: Given binary tree [3, 9, 20, null, null, 15, 7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

But i am trying it a new way in JavaScript and not going entirely by their solution. So far i am able to print the arrays but

How can different levels be printed in new rows

Below is my code so far:

var levelOrder = function(root) {
let output = [];
let queue = [];
let currentNode = root;
queue.push(currentNode);
let currentLevel = 1;
while(queue.length){
    
    currentNode = queue.shift();
    currentLevel--; //this will ensure we are adding new lines only on next level
    output.push(currentNode);
    
    if(currentNode.left){
        queue.push(currentNode.left);
    }
    if(currentNode.right){
        queue.push(currentNode.right);
    }
    
    if(currentLevel = 0){
        output = output + '/n'; //Insert a new line
        currentLevel = queue.length; //2
    }
}
return output;
};

Input: [3,9,20,null,null,15,7],

Expected Output:
[
[3],
[9,20],
[15,7]
]

LeetCode Question Link: BinaryTreeTraversalUsingBFS

like image 255
winterishere Avatar asked Jul 07 '20 22:07

winterishere


2 Answers

I think you're almost there. Not sure what output = output + '/n'; is for though.

This'd pass through:

var levelOrder = function(root) {
    const levels = []

    if(!root) {
        return levels
    }

    const queue = [root]
    while (queue.length){
       const queueLength = queue.length
       const level = []

       for(let i = 0; i < queueLength; i++){

           const node = queue.shift()

           if(node.left){
               queue.push(node.left)
           }
           if(node.right){
               queue.push(node.right)
           }

           level.push(node.val)
       }
       levels.push(level)
   }
    return levels
}

References

  • For additional details, you can see the Discussion Board. There are plenty of accepted solutions with a variety of languages and explanations, efficient algorithms, as well as asymptotic time/space complexity analysis1, 2 in there.
like image 193
Emma Avatar answered Sep 29 '22 08:09

Emma


Base on your codebase, I modify it for work.

  • Add an index for increasing the output index.
  • Use strict equality operator instead of assignment variable.
  • Remove output = output + '/n', because output is an array.

var levelOrder = function (root) {
  let output = [];
  let queue = [];
  let currentNode = root;
  queue.push(currentNode);
  let currentLevel = 1;
  let index = 0; // Add an index for increasing the output index

  while (queue.length) {

    currentNode = queue.shift();
    currentLevel--;

    if (!output[index]) { // Set default is an array for each output element in first time
      output[index] = [];
    }

    output[index].push(currentNode.val);

    if (currentNode.left) {
      queue.push(currentNode.left);
    }

    if (currentNode.right) {
      queue.push(currentNode.right);
    }

    if (currentLevel === 0) { // Use strict equality operator to compare 0
      index++; // increase index
      currentLevel = queue.length;
    }
  }

  return output;
};
like image 29
Alvin Avatar answered Sep 29 '22 09:09

Alvin