0
0
DSA Typescriptprogramming~10 mins

BST Inorder Predecessor 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 find the maximum node in the left subtree.

DSA Typescript
function findMaxNode(root: TreeNode | null): TreeNode | null {
  let current = root;
  while (current && current.[1]) {
    current = current.[1];
  }
  return current;
}
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cright
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' will find the minimum, not maximum.
Accessing a non-existent property like 'parent' or 'next'.
2fill in blank
medium

Complete the code to move up the tree to find the inorder predecessor when left subtree is absent.

DSA Typescript
function inorderPredecessor(root: TreeNode | null, node: TreeNode): TreeNode | null {
  if (node.left) {
    return findMaxNode(node.[1]);
  }
  let predecessor = null;
  let current = root;
  while (current) {
    if (node.val > current.val) {
      predecessor = current;
      current = current.[2];
    } else if (node.val < current.val) {
      current = current.left;
    } else {
      break;
    }
  }
  return predecessor;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' for the subtree.
Moving left when the value is greater instead of right.
3fill in blank
hard

Fix the error in the code that incorrectly updates the predecessor inside the loop.

DSA Typescript
while (current) {
  if (node.val > current.val) {
    predecessor = current;
    current = current.[1];
  } else {
    current = current.left;
  }
}
Drag options to blanks, or click blank then click option'
Aleft
Bnext
Cparent
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' causes incorrect traversal.
Not updating predecessor correctly.
4fill in blank
hard

Fill both blanks to complete the function that returns the inorder predecessor of a node in BST.

DSA Typescript
function inorderPredecessor(root: TreeNode | null, node: TreeNode): TreeNode | null {
  if (node.[1]) {
    return findMaxNode(node.[1]);
  }
  let predecessor = null;
  let current = root;
  while (current) {
    if (node.val > current.val) {
      predecessor = current;
      current = current.[2];
    } else {
      current = current.left;
    }
  }
  return predecessor;
}
Drag options to blanks, or click blank then click option'
Aleft
Bright
Cparent
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' for the subtree.
Moving left instead of right when node value is greater.
5fill in blank
hard

Fill all three blanks to complete the code that finds the inorder predecessor using BST properties.

DSA Typescript
function inorderPredecessor(root: TreeNode | null, node: TreeNode): TreeNode | null {
  if (node.[1]) {
    return findMaxNode(node.[1]);
  }
  let predecessor = null;
  let current = root;
  while (current) {
    if (node.val [2] current.val) {
      predecessor = current;
      current = current.[3];
    } else {
      current = current.left;
    }
  }
  return predecessor;
}
Drag options to blanks, or click blank then click option'
Aleft
B>
Cright
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the comparison.
Moving left instead of right when node value is greater.