Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the bottom view of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the right side
B. Nodes visible when the tree is viewed from the top
C. Nodes visible when the tree is viewed from the bottom
D. Nodes visible when the tree is viewed from the left side

Solution

  1. Step 1: Understand the bottom view concept

    The bottom view shows nodes visible when looking at the tree from below, meaning the lowest nodes at each horizontal distance.
  2. Step 2: Compare with other views

    Top view shows nodes from above, left and right views show nodes from sides, so bottom view is unique for bottom perspective.
  3. Final Answer:

    Nodes visible when the tree is viewed from the bottom -> Option C
  4. Quick Check:

    Bottom view = nodes visible from below [OK]
Hint: Bottom view shows lowest nodes at each horizontal distance [OK]
Common Mistakes:
  • Confusing bottom view with top view
  • Thinking bottom view shows all leaf nodes
  • Mixing bottom view with side views
2. Which of the following JavaScript code snippets correctly initializes a Map to store horizontal distances and nodes for bottom view?
easy
A. const map = new Map();
B. const map = {};
C. const map = new Array();
D. const map = new Set();

Solution

  1. Step 1: Identify suitable data structure for key-value pairs

    Map is ideal for storing horizontal distance as key and node data as value with easy insertion and retrieval.
  2. Step 2: Check other options

    Object ({}) can be used but Map is preferred for ordered keys and better methods; Array and Set are not suitable for key-value pairs.
  3. Final Answer:

    const map = new Map(); -> Option A
  4. Quick Check:

    Use Map for horizontal distance mapping [OK]
Hint: Use Map() for key-value storage in bottom view [OK]
Common Mistakes:
  • Using Array instead of Map for key-value pairs
  • Using Set which stores only unique values, not key-value
  • Using plain object without considering key types
3. Consider the following binary tree and code snippet to print its bottom view:
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

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);

// Bottom view logic omitted for brevity
// Assume correct implementation

// Output bottom view nodes in order of horizontal distance
// What is the bottom view output?
medium
A. [5, 8, 3, 14, 25]
B. [5, 10, 3, 14, 25]
C. [5, 10, 3, 22, 25]
D. [8, 10, 3, 14, 25]

Solution

  1. Step 1: Assign horizontal distances and track bottom nodes

    Assign horizontal distances: 20(0), 8(-1), 22(1), 5(-2), 3(0), 25(2), 10(-1), 14(1). Bottom view keeps last node at each horizontal distance.
  2. Step 2: Determine bottom nodes at each horizontal distance

    At -2: 5; at -1: 10 (overwrites 8); at 0: 3 (overwrites 20); at 1: 14 (overwrites 22); at 2: 25.
  3. Final Answer:

    [5, 10, 3, 14, 25] -> Option B
  4. Quick Check:

    Bottom view nodes = [5,10,3,14,25] [OK]
Hint: Track last node at each horizontal distance [OK]
Common Mistakes:
  • Using first node instead of last at horizontal distance
  • Mixing horizontal distances incorrectly
  • Ignoring overwriting nodes at same horizontal distance
4. The following code attempts to print the bottom view of a binary tree but produces incorrect output. What is the main issue?
function bottomView(root) {
  const map = new Map();
  const queue = [];
  queue.push({ node: root, hd: 0 });

  while (queue.length > 0) {
    const { node, hd } = queue.shift();
    if (!map.has(hd)) {
      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.values());
}
medium
A. It stores only the first node at each horizontal distance, not the bottom-most
B. It uses shift() which is inefficient for queue
C. It does not handle null root input
D. It uses Map instead of Object for storage

Solution

  1. Step 1: Analyze map update condition

    The code sets map value only if horizontal distance key is not present, so it keeps first node encountered at that hd.
  2. Step 2: Understand bottom view requirement

    Bottom view requires last node at each horizontal distance (lowest level), so map should update value every time a node at same hd is found.
  3. Final Answer:

    It stores only the first node at each horizontal distance, not the bottom-most -> Option A
  4. Quick Check:

    Bottom view needs last node per hd, code keeps first [OK]
Hint: Update map value for every node at horizontal distance [OK]
Common Mistakes:
  • Not updating map on repeated horizontal distances
  • Confusing top view logic with bottom view
  • Ignoring queue inefficiency but that doesn't affect correctness
5. Given a binary tree where multiple nodes share the same horizontal distance and level, how should the bottom view algorithm decide which node to display?
hard
A. Display the node with the smallest value at that horizontal distance
B. Display the leftmost node at that horizontal distance and level
C. Display the rightmost node at that horizontal distance and level
D. Display the node encountered last in level order traversal at that horizontal distance

Solution

  1. Step 1: Understand node conflicts at same horizontal distance and level

    When nodes share horizontal distance and level, bottom view shows the one visible from bottom, which is the one encountered last in level order traversal.
  2. Step 2: Reason about traversal order

    Level order traversal processes nodes left to right at each level, so last encountered node at hd and level overwrites previous.
  3. Final Answer:

    Display the node encountered last in level order traversal at that horizontal distance -> Option D
  4. Quick Check:

    Bottom view uses last node in level order per hd [OK]
Hint: Use last node in level order traversal for bottom view [OK]
Common Mistakes:
  • Choosing leftmost or rightmost arbitrarily
  • Choosing node by value instead of position
  • Ignoring traversal order impact
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard