Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a binary tree node class with left and right children.
DSA Typescript
class Node { value: number; left: Node | null; right: Node | null; constructor(value: number) { this.value = value; this.left = [1]; this.right = null; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or false instead of null for child nodes.
Leaving the child uninitialized.
✗ Incorrect
In TypeScript, to indicate no child node, we use null to initialize left and right pointers.
2fill in blank
mediumComplete the code to insert a node into the binary tree at the left child.
DSA Typescript
function insertLeft(parent: Node, value: number): void {
parent.left = new Node([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent.value instead of the new value.
Assigning null or undefined instead of a new node.
✗ Incorrect
We create a new node with the given value to insert as the left child.
3fill in blank
hardFix the error in the function to compute the top view by completing the missing code to track horizontal distance.
DSA Typescript
function topView(root: Node): number[] {
if (!root) return [];
const queue: Array<[Node, number]> = [[root, 0]];
const map = new Map<number, number>();
while (queue.length > 0) {
const [node, hd] = queue.shift()!;
if (!map.has([1])) {
map.set(hd, node.value);
}
if (node.left) queue.push([node.left, hd - 1]);
if (node.right) queue.push([node.right, hd + 1]);
}
const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
const result = sortedKeys.map(key => map.get(key)!);
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.value instead of hd in the map.
Using queue length or node.left as key mistakenly.
✗ Incorrect
We check if the horizontal distance 'hd' is already in the map to decide if we add the node's value.
4fill in blank
hardFill both blanks to correctly update the queue with child nodes and their horizontal distances.
DSA Typescript
if (node.left) queue.push([node.left, [1]]); if (node.right) queue.push([node.right, [2]]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same horizontal distance for both children.
Using node.value instead of hd adjustments.
✗ Incorrect
Left child is at horizontal distance one less than parent, right child is one more.
5fill in blank
hardFill all three blanks to return the top view values sorted by horizontal distance.
DSA Typescript
const sortedKeys = Array.from(map.keys()).sort((a, b) => [1]); const result = sortedKeys.map(key => map.get([2])!); return [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting keys descending (b - a) instead of ascending.
Returning keys instead of mapped values.
Using wrong variable names.
✗ Incorrect
Sort keys ascending (a - b), map keys to values, then return the result array.