Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

BST Inorder Successor in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find the inorder successor of a node with right subtree
Given the BST and the node with value 15, what is the value of its inorder successor?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

// BST structure:
//       20
//      /  \
//    10    30
//      \     \
//      15    40

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);
root.left.right = new TreeNode(15);
root.right.right = new TreeNode(40);

function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (!root) return null;
  if (p.right) {
    let curr = p.right;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let successor: TreeNode | null = null;
  let curr = root;
  while (curr) {
    if (p.val < curr.val) {
      successor = curr;
      curr = curr.left;
    } else if (p.val > curr.val) {
      curr = curr.right;
    } else {
      break;
    }
  }
  return successor;
}

const node = root.left.right; // Node with value 15
const successor = inorderSuccessor(root, node);
console.log(successor ? successor.val : null);
A20
B30
C15
Dnull
Attempts:
2 left
💡 Hint
If the node has a right child, the successor is the leftmost node in the right subtree.
❓ Predict Output
intermediate
2:00remaining
Inorder successor of a node without right subtree
What is the inorder successor of the node with value 10 in the given BST?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

// BST structure:
//       20
//      /  \
//    10    30
//      \     \
//      15    40

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);
root.left.right = new TreeNode(15);
root.right.right = new TreeNode(40);

function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (!root) return null;
  if (p.right) {
    let curr = p.right;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let successor: TreeNode | null = null;
  let curr = root;
  while (curr) {
    if (p.val < curr.val) {
      successor = curr;
      curr = curr.left;
    } else if (p.val > curr.val) {
      curr = curr.right;
    } else {
      break;
    }
  }
  return successor;
}

const node = root.left; // Node with value 10
const successor = inorderSuccessor(root, node);
console.log(successor ? successor.val : null);
A15
B20
C10
Dnull
Attempts:
2 left
💡 Hint
If the node has a right child, the successor is the leftmost node in the right subtree.
🔧 Debug
advanced
2:00remaining
Identify the error in inorder successor function
What error will this code produce when trying to find the inorder successor of node 30?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);
root.left.right = new TreeNode(15);
root.right.right = new TreeNode(40);

function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (!root) return null;
  if (p.right) {
    let curr = p.right;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let successor: TreeNode | null = null;
  let curr = root;
  while (curr) {
    if (p.val < curr.val) {
      successor = curr;
      curr = curr.left;
    } else if (p.val > curr.val) {
      curr = curr.right;
    } else {
      break;
    }
  }
  return successor;
}

const node = root.right; // Node with value 30
const successor = inorderSuccessor(root, node);
console.log(successor ? successor.val : null);
ASyntaxError
Bnull
CTypeError
D40
Attempts:
2 left
💡 Hint
Check if the node has a right subtree and find the leftmost node there.
❓ Predict Output
advanced
2:00remaining
Inorder successor of the maximum node in BST
What is the inorder successor of the node with value 40 in the BST?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

// BST structure:
//       20
//      /  \
//    10    30
//      \     \
//      15    40

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);
root.left.right = new TreeNode(15);
root.right.right = new TreeNode(40);

function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  if (!root) return null;
  if (p.right) {
    let curr = p.right;
    while (curr.left) curr = curr.left;
    return curr;
  }
  let successor: TreeNode | null = null;
  let curr = root;
  while (curr) {
    if (p.val < curr.val) {
      successor = curr;
      curr = curr.left;
    } else if (p.val > curr.val) {
      curr = curr.right;
    } else {
      break;
    }
  }
  return successor;
}

const node = root.right.right; // Node with value 40
const successor = inorderSuccessor(root, node);
console.log(successor ? successor.val : null);
A30
B40
Cnull
D20
Attempts:
2 left
💡 Hint
The maximum node in BST has no inorder successor.
🧠 Conceptual
expert
2:00remaining
Inorder successor when node is root with no right subtree
In a BST, if the root node has no right subtree, what determines its inorder successor?
AThe leftmost node in the left subtree
BThe lowest ancestor whose left child is also an ancestor of the node
CThe rightmost node in the left subtree
DThere is no inorder successor
Attempts:
2 left
💡 Hint
Think about how inorder traversal visits nodes in ascending order.

Practice

(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The root node of the BST
D. The left child of the given node

Solution

  1. Step 1: Understand inorder traversal order

    Inorder traversal visits nodes in ascending order of their keys in a BST.
  2. Step 2: Define inorder successor

    The inorder successor of a node is the next node visited after it in inorder traversal, which is the smallest node with a key greater than the current node.
  3. Final Answer:

    The node with the smallest key greater than the given node's key -> Option A
  4. Quick Check:

    Inorder successor = next bigger node [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
  • Confusing successor with predecessor
  • Thinking successor is always right child
  • Assuming successor is parent node
2. Which of the following TypeScript function signatures correctly represents a function to find the inorder successor in a BST node?
easy
A. function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null
B. function inorderSuccessor(p: TreeNode): TreeNode
C. function inorderSuccessor(root: TreeNode): void
D. function inorderSuccessor(root: TreeNode, p: TreeNode): number

Solution

  1. Step 1: Analyze parameters needed

    To find inorder successor, we need the BST root and the target node p.
  2. Step 2: Analyze return type

    The function returns the successor node or null if none exists.
  3. Final Answer:

    function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null -> Option A
  4. Quick Check:

    Correct params and return type [OK]
Hint: Successor function needs root and target node, returns node or null [OK]
Common Mistakes:
  • Missing root parameter
  • Wrong return type (void or number)
  • Not allowing null root
3. Given the BST below and the node with value 5, what is the inorder successor?
      6
     / \
    2   8
   / \   \
  0   4   9
     / \
    3   5
medium
A. Node with value 8
B. Node with value 6
C. Node with value 4
D. Node with value 9

Solution

  1. Step 1: Locate node 5 and check right subtree

    Node 5 has no right child, so successor is one of its ancestors.
  2. Step 2: Find lowest ancestor whose left child is ancestor of node 5

    Node 6 is the first ancestor where node 5 is in left subtree, so successor is node 6.
  3. Final Answer:

    Node with value 6 -> Option B
  4. Quick Check:

    Successor of 5 is 6 [OK]
Hint: If no right child, go up to first bigger ancestor [OK]
Common Mistakes:
  • Choosing right child of root instead of ancestor
  • Picking node 8 which is too big
  • Confusing predecessor with successor
4. Consider this TypeScript code snippet to find the inorder successor of a node in a BST:
function inorderSuccessor(root: TreeNode | null, p: TreeNode): TreeNode | null {
  let successor: TreeNode | null = null;
  while (root) {
    if (p.val < root.val) {
      successor = root;
      root = root.left;
    } else {
      if (p.val === root.val) {
        return successor;
      }
      root = root.right;
    }
  }
  return successor;
}
What is the bug in this code?
medium
A. It may cause infinite loop if root is null
B. It incorrectly updates the successor when p.val > root.val
C. It does not handle the case when node p has a right subtree
D. It returns the predecessor instead of successor

Solution

  1. Step 1: Understand code logic

    The code moves down the tree to find the smallest node greater than p.val, updating successor accordingly.
  2. Step 2: Identify missing case

    If p has a right subtree, the successor is the leftmost node in that subtree, but this code ignores that and only searches from root.
  3. Final Answer:

    It does not handle the case when node p has a right subtree -> Option C
  4. Quick Check:

    Right subtree check missing [OK]
Hint: Check right subtree first for successor [OK]
Common Mistakes:
  • Ignoring right subtree of p
  • Assuming successor always in ancestors
  • Confusing successor with predecessor
5. You have a BST and want to find the inorder successor of a node p. The node p has a right child. Which of the following is the correct approach to find the inorder successor?
hard
A. Return the root of the BST
B. Return the rightmost node in p's left subtree
C. Return the parent of p
D. Return the leftmost node in p's right subtree

Solution

  1. Step 1: Check if p has right subtree

    If p has a right child, the inorder successor is the smallest node in that right subtree.
  2. Step 2: Find leftmost node in right subtree

    Traverse left children starting from p's right child until no more left child exists.
  3. Final Answer:

    Return the leftmost node in p's right subtree -> Option D
  4. Quick Check:

    Successor in right subtree's leftmost node [OK]
Hint: Successor with right child = leftmost node in right subtree [OK]
Common Mistakes:
  • Returning parent instead of leftmost right subtree node
  • Returning rightmost left subtree node
  • Returning root regardless of p
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard