0
0
DSA Typescriptprogramming~10 mins

BST Inorder Successor 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 return the left child of a node.

DSA Typescript
function getLeftChild(node: TreeNode | null): TreeNode | null {
  return node[1];
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using .right instead of .left
Using .parent or .value which are not children
2fill in blank
medium

Complete the code to find the minimum node in a BST subtree.

DSA Typescript
function findMin(node: TreeNode | null): TreeNode | null {
  while (node && node[1]) {
    node = node.left;
  }
  return node;
}
Drag options to blanks, or click blank then click option'
A.value
B.right
C.parent
D.left
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.right instead of node.left
Returning node without checking children
3fill in blank
hard

Fix the error in the code to correctly find the inorder successor when the node has no right child.

DSA Typescript
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  let successor: TreeNode | null = null;
  while (root) {
    if (p.value < root.value) {
      successor = root;
      root = root[1];
    } else {
      root = root.right;
    }
  }
  return successor;
}
Drag options to blanks, or click blank then click option'
A.right
B.left
C.parent
D.value
Attempts:
3 left
💡 Hint
Common Mistakes
Moving right instead of left when p.value < root.value
Using root.parent which is not available
4fill in blank
hard

Fill both blanks to complete the function that finds the inorder successor of a node in a BST.

DSA Typescript
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (p.right) {
    return findMin(p[1]);
  }
  let successor: TreeNode | null = null;
  while (root) {
    if (p.value < root.value) {
      successor = root;
      root = root[2];
    } else {
      root = root.right;
    }
  }
  return successor;
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using p.left instead of p.right for right subtree
Moving root.right instead of root.left in the loop
5fill in blank
hard

Fill all three blanks to complete the helper function and the inorder successor logic.

DSA Typescript
function findMin(node: TreeNode | null): TreeNode | null {
  while (node && node[1]) {
    node = node[2];
  }
  return node;
}

function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (p.right) {
    return findMin(p[3]);
  }
  let successor: TreeNode | null = null;
  while (root) {
    if (p.value < root.value) {
      successor = root;
      root = root.left;
    } else {
      root = root.right;
    }
  }
  return successor;
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.right instead of node.left in findMin
Using p.left instead of p.right for successor