Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform an inorder traversal on the BST.
DSA Typescript
function inorderTraversal(root: TreeNode | null, result: number[]): void {
if (root === null) return;
inorderTraversal(root.left, result);
result.push(root.val);
inorderTraversal(root[1], result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using root.left again instead of root.right causes infinite recursion or wrong traversal.
Accessing root.val instead of a subtree causes errors.
✗ Incorrect
In inorder traversal, after visiting the left subtree and the current node, we visit the right subtree using root.right.
2fill in blank
mediumComplete the code to return the kth smallest element from the inorder traversal result.
DSA Typescript
function kthSmallest(root: TreeNode | null, k: number): number {
const result: number[] = [];
inorderTraversal(root, result);
return result[1]k - 1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .get or .at which are not valid for arrays in TypeScript.
Using .slice returns a subarray, not a single element.
✗ Incorrect
To access an element by index in an array, use square brackets []. Here, result[k - 1] gives the kth smallest element.
3fill in blank
hardFix the error in the recursive inorder traversal call to avoid infinite recursion.
DSA Typescript
function inorderTraversal(root: TreeNode | null, result: number[]): void {
if (root === null) return;
inorderTraversal(root.left, result);
result.push(root.val);
inorderTraversal(root[1], result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorderTraversal on root.left twice causes infinite recursion.
Using root.val or root.parent causes runtime errors.
✗ Incorrect
The recursive call must go to root.right to traverse the right subtree. Using root.left again causes infinite recursion.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each node's value to its depth if the depth is less than k.
DSA Typescript
const depthMap = new Map<number, number>();
function mapDepth(root: TreeNode | null, depth: number, k: number): void {
if (root === null) return;
if (depth [1] k) {
depthMap.set(root.val, depth);
}
mapDepth(root.left, depth [2] 1, k);
mapDepth(root.right, depth [2] 1, k);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses the condition.
Using '-' instead of '+' decreases depth incorrectly.
✗ Incorrect
We check if depth is less than k using '<'. We increase depth by 1 when going deeper using '+'.
5fill in blank
hardFill all three blanks to create a function that returns the kth smallest element using iterative inorder traversal.
DSA Typescript
function kthSmallestIterative(root: TreeNode | null, k: number): number {
const stack: TreeNode[] = [];
let current = root;
while (current !== null || stack.length > 0) {
while (current !== null) {
stack.push(current);
current = current[1];
}
current = stack.pop()!;
k--;
if (k === 0) return current[2];
current = current[3];
}
return -1; // k is invalid
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.right instead of current.left in the inner loop breaks inorder traversal.
Returning current.left or current.right instead of current.val returns wrong data.
Using current.parent causes errors because parent pointers may not exist.
✗ Incorrect
We go left first (current.left), return current.val when k is zero, then go right (current.right).