0
0
DSA Javascriptprogramming~20 mins

Tree Traversal Level Order BFS in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Level Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this level order traversal?
Given the binary tree below, what is the printed output of the level order traversal function?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function levelOrder(root) {
  if (!root) return [];
  const queue = [root];
  const result = [];
  while (queue.length > 0) {
    const node = queue.shift();
    result.push(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
  return result;
}

// Tree structure:
//       10
//      /  \
//     5    15
//    / \     \
//   3   7     18

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right = new Node(18);

console.log(levelOrder(root));
A[10, 5, 3, 7, 15, 18]
B[10, 5, 15, 3, 7, 18]
C[10, 15, 5, 18, 7, 3]
D[3, 7, 5, 18, 15, 10]
Attempts:
2 left
💡 Hint
Think about visiting nodes level by level from left to right.
Predict Output
intermediate
2:00remaining
What is the output of this level order traversal with null nodes?
What will be the output of the level order traversal function when the tree has some missing children?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function levelOrder(root) {
  if (!root) return [];
  const queue = [root];
  const result = [];
  while (queue.length > 0) {
    const node = queue.shift();
    result.push(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
  return result;
}

// Tree structure:
//       1
//      / \
//     2   null
//    / \
// null 3

const root = new Node(1);
root.left = new Node(2);
root.left.right = new Node(3);

console.log(levelOrder(root));
A[1, 2, 3]
B[1, 2]
C[1, 3, 2]
D[2, 1, 3]
Attempts:
2 left
💡 Hint
Null children are skipped and not added to the queue.
🧠 Conceptual
advanced
1:00remaining
Which data structure is essential for implementing level order traversal?
To perform a level order traversal (BFS) on a binary tree, which data structure is most suitable to keep track of nodes to visit next?
AQueue
BSet
CHash Map
DStack
Attempts:
2 left
💡 Hint
Think about visiting nodes in the order they appear level by level.
🔧 Debug
advanced
1:30remaining
What error does this level order traversal code produce?
Identify the error when running this level order traversal code snippet.
DSA Javascript
function levelOrder(root) {
  if (!root) return [];
  const queue = [];
  const result = [];
  queue.push(root);
  while (queue.length > 0) {
    const node = queue.pop();
    result.push(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
  return result;
}
ASyntaxError due to missing semicolon
BInfinite loop because queue never empties
CTypeError because queue is undefined
DThe traversal order is reversed, not level order
Attempts:
2 left
💡 Hint
Check how nodes are removed from the queue.
🚀 Application
expert
2:00remaining
How many nodes are visited in this level order traversal?
Given the following tree, how many nodes will the level order traversal visit?
DSA Javascript
// Tree structure:
//       8
//      / \
//     4   12
//    /   /  \
//   2   10  14
//        
//       11

class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(8);
root.left = new Node(4);
root.right = new Node(12);
root.left.left = new Node(2);
root.right.left = new Node(10);
root.right.right = new Node(14);
root.right.left.right = new Node(11);

function levelOrder(root) {
  if (!root) return [];
  const queue = [root];
  const result = [];
  while (queue.length > 0) {
    const node = queue.shift();
    result.push(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
  return result;
}

const visitedNodes = levelOrder(root).length;
console.log(visitedNodes);
A8
B6
C7
D5
Attempts:
2 left
💡 Hint
Count all nodes including the root and all children.