0
0
DSA Javascriptprogramming~10 mins

Bottom View of Binary Tree in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node in the binary tree.

DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

const root = new Node([1]);
Drag options to blanks, or click blank then click option'
Anull
B10
C'root'
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' or 'undefined' as the node's data value.
Using a string like 'root' instead of a number.
2fill in blank
medium

Complete the code to add a left child to the root node.

DSA Javascript
root.left = new Node([1]);
Drag options to blanks, or click blank then click option'
A5
Bundefined
C'left'
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'null' or 'undefined' instead of a new node.
Using a string instead of a number for the node's data.
3fill in blank
hard

Fix the error in the function to calculate the bottom view of the binary tree by completing the missing code.

DSA Javascript
function bottomView(root) {
  if (!root) return [];
  const map = new Map();
  const queue = [];
  queue.push({ node: root, hd: 0 });

  while (queue.length > 0) {
    const { node, hd } = queue.shift();
    map.set(hd, node.data);

    if (node.left) queue.push({ node: node.left, hd: hd [1] 1 });
    if (node.right) queue.push({ node: node.right, hd: hd + 1 });
  }

  const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
  return sortedKeys.map(key => map.get(key));
}
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for left child's horizontal distance.
Using multiplication or division operators incorrectly.
4fill in blank
hard

Fill both blanks to complete the code that updates the map and processes the queue for bottom view calculation.

DSA Javascript
while (queue.length > 0) {
  const { node, hd } = queue.shift();
  map.set([1], [2]);

  if (node.left) queue.push({ node: node.left, hd: hd - 1 });
  if (node.right) queue.push({ node: node.right, hd: hd + 1 });
}
Drag options to blanks, or click blank then click option'
Ahd
Bnode.data
Cnode
Dhd + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using node instead of node.data as map value.
Using hd + 1 as key instead of hd.
5fill in blank
hard

Fill all three blanks to complete the code that returns the bottom view array sorted by horizontal distance.

DSA Javascript
const sortedKeys = Array.from(map.keys()).sort((a, b) => a [1] b);
return sortedKeys.map(key => map.[2](key));
Drag options to blanks, or click blank then click option'
A-
Bget
C+
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in sort comparator.
Using map.set instead of map.get to retrieve values.