0
0
DSA Javascriptprogramming~20 mins

Bottom View of Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bottom View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Bottom View for a Simple Binary Tree
What is the output of the bottom view traversal for the given binary tree?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

function bottomView(root) {
  if (!root) return [];
  let map = new Map();
  let queue = [{ node: root, hd: 0 }];
  while (queue.length) {
    let { node, hd } = queue.shift();
    map.set(hd, node.data);
    if (node.left) queue.push({ node: node.left, hd: hd - 1 });
    if (node.right) queue.push({ node: node.right, hd: hd + 1 });
  }
  return Array.from(map.entries())
    .sort((a, b) => a[0] - b[0])
    .map(entry => entry[1]);
}

// Tree structure:
//       1
//      / \
//     2   3
//      \   \
//       4   5

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

console.log(bottomView(root));
A[2, 4, 1, 5]
B[2, 4, 3, 5]
C[2, 1, 3, 5]
D[4, 2, 3, 5]
Attempts:
2 left
💡 Hint
Remember bottom view shows the last node at each horizontal distance from left to right.
Predict Output
intermediate
2:00remaining
Bottom View Output for a Tree with Multiple Levels
What is the bottom view output for the following binary tree?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

function bottomView(root) {
  if (!root) return [];
  let map = new Map();
  let queue = [{ node: root, hd: 0 }];
  while (queue.length) {
    let { node, hd } = queue.shift();
    map.set(hd, node.data);
    if (node.left) queue.push({ node: node.left, hd: hd - 1 });
    if (node.right) queue.push({ node: node.right, hd: hd + 1 });
  }
  return Array.from(map.entries())
    .sort((a, b) => a[0] - b[0])
    .map(entry => entry[1]);
}

// Tree structure:
//         20
//        /  \
//      8     22
//     / \      \
//    5   3      25
//       / \     
//      10  14

const root = new Node(20);
root.left = new Node(8);
root.right = new Node(22);
root.left.left = new Node(5);
root.left.right = new Node(3);
root.right.right = new Node(25);
root.left.right.left = new Node(10);
root.left.right.right = new Node(14);

console.log(bottomView(root));
A[5, 8, 3, 14, 25]
B[5, 8, 10, 14, 25]
C[5, 10, 14, 22, 25]
D[5, 10, 3, 14, 25]
Attempts:
2 left
💡 Hint
Track horizontal distances and keep the last node at each distance.
🔧 Debug
advanced
2:00remaining
Identify the Error in Bottom View Implementation
What error will this code produce when trying to compute the bottom view of a binary tree?
DSA Javascript
function bottomView(root) {
  if (!root) return [];
  let map = new Map();
  let queue = [{ node: root, hd: 0 }];
  while (queue.length) {
    let { node, hd } = queue.pop();
    map.set(hd, node.data);
    if (node.left) queue.push({ node: node.left, hd: hd - 1 });
    if (node.right) queue.push({ node: node.right, hd: hd + 1 });
  }
  return Array.from(map.entries())
    .sort((a, b) => a[0] - b[0])
    .map(entry => entry[1]);
}
AInfinite loop because queue never empties
BSyntaxError due to incorrect destructuring
CThe output will be reversed bottom view nodes
DTypeError because node is undefined
Attempts:
2 left
💡 Hint
Check how queue is processed: shift vs pop affects order.
🧠 Conceptual
advanced
1:30remaining
Understanding Horizontal Distance in Bottom View
In the bottom view of a binary tree, what does the horizontal distance (hd) represent?
AThe distance of the node from the root along the horizontal axis, with left as negative and right as positive
BThe vertical level of the node in the tree
CThe depth of the node from the root
DThe number of nodes in the subtree rooted at the node
Attempts:
2 left
💡 Hint
Think about how left and right children affect horizontal position.
🚀 Application
expert
3:00remaining
Bottom View Output for Complex Tree with Overlapping Nodes
Given the binary tree below, what is the bottom view output?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

function bottomView(root) {
  if (!root) return [];
  let map = new Map();
  let queue = [{ node: root, hd: 0 }];
  while (queue.length) {
    let { node, hd } = queue.shift();
    map.set(hd, node.data);
    if (node.left) queue.push({ node: node.left, hd: hd - 1 });
    if (node.right) queue.push({ node: node.right, hd: hd + 1 });
  }
  return Array.from(map.entries())
    .sort((a, b) => a[0] - b[0])
    .map(entry => entry[1]);
}

// Tree structure:
//           1
//         /   \
//        2     3
//       / \   / \
//      4   5 6   7
//           \     \
//            8     9

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.left.right.right = new Node(8);
root.right.right.right = new Node(9);

console.log(bottomView(root));
A[4, 2, 6, 8, 7, 9]
B[4, 2, 5, 6, 7, 9]
C[4, 2, 6, 8, 9]
D[4, 2, 6, 8, 7]
Attempts:
2 left
💡 Hint
Track horizontal distances carefully and note which nodes overwrite others at the same hd.