0
0
DSA Typescriptprogramming~10 mins

Kth Smallest Element in BST in DSA Typescript - Interactive Practice

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

Complete 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'
A.parent
B.left
C.val
D.right
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.
2fill in blank
medium

Complete 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'
A.at
B[
C.get
D.slice
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.
3fill in blank
hard

Fix 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'
A.right
B.left
C.val
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Calling inorderTraversal on root.left twice causes infinite recursion.
Using root.val or root.parent causes runtime errors.
4fill in blank
hard

Fill 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'
A<
B>
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses the condition.
Using '-' instead of '+' decreases depth incorrectly.
5fill in blank
hard

Fill 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'
A.left
B.val
C.right
D.parent
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.