0
0
DSA Javascriptprogramming~20 mins

BST Inorder Successor in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 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 node 15 in this BST

Given the BST below, what is the inorder successor of the node with value 15?

BST structure:

       20
      /  \
    10    30
      \     \
      15    40
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

// BST creation
const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
root.left.right = new Node(15);
root.right.right = new Node(40);

function inorderSuccessor(root, p) {
  let successor = null;
  while (root) {
    if (p.val < root.val) {
      successor = root;
      root = root.left;
    } else {
      root = root.right;
    }
  }
  return successor ? successor.val : null;
}

console.log(inorderSuccessor(root, root.left.right));
A20
B30
C15
Dnull
Attempts:
2 left
💡 Hint

Inorder successor is the next bigger value in BST.

Predict Output
intermediate
2:00remaining
Output of inorder successor for node 70 in BST

What is the output of the inorder successor function when called with node 70 in the BST below?

       25
      /  \
    15    50
   /  \     \
 10   22    70
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(25);
root.left = new Node(15);
root.right = new Node(50);
root.left.left = new Node(10);
root.left.right = new Node(22);
root.right.right = new Node(70);

function inorderSuccessor(root, p) {
  let successor = null;
  while (root) {
    if (p.val < root.val) {
      successor = root;
      root = root.left;
    } else {
      root = root.right;
    }
  }
  return successor ? successor.val : null;
}

console.log(inorderSuccessor(root, root.right.right));
A50
Bnull
C70
D25
Attempts:
2 left
💡 Hint

Check if node 70 has any bigger node in BST.

🔧 Debug
advanced
2:00remaining
Identify the error in inorder successor code

What error will this code produce when trying to find the inorder successor of node 10?

DSA Javascript
function inorderSuccessor(root, p) {
  let successor = null;
  while (root !== null) {
    if (p.val <= root.val) {
      successor = root;
      root = root.left;
    } else {
      root = root.right;
    }
  }
  return successor.val;
}

// BST setup
const root = { val: 20, left: { val: 10, right: { val: 15 } }, right: { val: 30 } };
console.log(inorderSuccessor(root, root.left));
A15
BReferenceError: successor is not defined
Cundefined
DTypeError: Cannot read property 'val' of null
Attempts:
2 left
💡 Hint

Check what happens if successor is null at return.

🧠 Conceptual
advanced
1:30remaining
Inorder successor when node has right subtree

In a BST, if a node has a right child, what is the inorder successor of that node?

AThe leftmost node in the right subtree
BThe parent node
CThe right child itself
DThe rightmost node in the left subtree
Attempts:
2 left
💡 Hint

Think about inorder traversal order.

Predict Output
expert
2:30remaining
Output of inorder successor for node 22 in complex BST

What is the output of the inorder successor function for node 22 in this BST?

          50
         /  \
       30    70
      /  \   / \
    20   40 60  80
      
       22
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(50);
root.left = new Node(30);
root.right = new Node(70);
root.left.left = new Node(20);
root.left.right = new Node(40);
root.right.left = new Node(60);
root.right.right = new Node(80);
root.left.left.right = new Node(22);

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

console.log(inorderSuccessor(root, root.left.left.right));
A50
B40
C30
D22
Attempts:
2 left
💡 Hint

Check if node 22 has right child, else find closest ancestor.