Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The root node should be initialized with a numeric value like 10 to represent the data stored in the node.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The left child node should be created with a numeric value like 5 to represent its data.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for left child's horizontal distance.
Using multiplication or division operators incorrectly.
✗ Incorrect
For the left child, the horizontal distance decreases by 1, so we subtract 1 from hd.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node instead of node.data as map value.
Using hd + 1 as key instead of hd.
✗ Incorrect
The map key is the horizontal distance (hd), and the value is the node's data for bottom view.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in sort comparator.
Using map.set instead of map.get to retrieve values.
✗ Incorrect
Sorting keys in ascending order uses subtraction (a - b). To get values from map, use map.get(key).