0
0
DSA Typescriptprogramming~20 mins

Kth Smallest Element in BST in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Kth Smallest Element Function
What is the output of the following TypeScript code that finds the 3rd smallest element in a BST?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = (val===undefined ? 0 : val);
    this.left = (left===undefined ? null : left);
    this.right = (right===undefined ? null : right);
  }
}

function kthSmallest(root: TreeNode | null, k: number): number {
  let count = 0;
  let result = -1;

  function inorder(node: TreeNode | null) {
    if (!node || result !== -1) return;
    inorder(node.left);
    count++;
    if (count === k) {
      result = node.val;
      return;
    }
    inorder(node.right);
  }

  inorder(root);
  return result;
}

const root = new TreeNode(5,
  new TreeNode(3,
    new TreeNode(2,
      new TreeNode(1), null),
    new TreeNode(4)),
  new TreeNode(6)
);

console.log(kthSmallest(root, 3));
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST visits nodes in ascending order.
🧠 Conceptual
intermediate
1:30remaining
Understanding Inorder Traversal in BST
Why does inorder traversal help find the kth smallest element in a BST?
ABecause inorder traversal visits nodes in ascending order of their values.
BBecause inorder traversal visits nodes in descending order of their values.
CBecause inorder traversal visits nodes randomly.
DBecause inorder traversal visits only leaf nodes.
Attempts:
2 left
💡 Hint
Think about how BST stores smaller values on the left and larger on the right.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Kth Smallest Element Code
What error will the following code produce when trying to find the 1st smallest element?
DSA Typescript
function kthSmallest(root: TreeNode | null, k: number): number {
  let count = 0;
  let result = -1;

  function inorder(node: TreeNode | null) {
    if (!node) return;
    inorder(node.left);
    if (count === k) {
      result = node.val;
      return;
    }
    count++;
    inorder(node.right);
  }

  inorder(root);
  return result;
}
AInfinite recursion
BThrows a runtime error
CReturns -1 (incorrect result)
DReturns the correct kth smallest element
Attempts:
2 left
💡 Hint
Check when count is incremented and when result is assigned.
Predict Output
advanced
2:00remaining
Output of Kth Smallest Element with Duplicate Values
What is the output of the following code when finding the 4th smallest element in a BST with duplicates?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = (val===undefined ? 0 : val);
    this.left = (left===undefined ? null : left);
    this.right = (right===undefined ? null : right);
  }
}

const root = new TreeNode(3,
  new TreeNode(1,
    null,
    new TreeNode(2)),
  new TreeNode(4,
    new TreeNode(4),
    null)
);

console.log(kthSmallest(root, 4));

function kthSmallest(root: TreeNode | null, k: number): number {
  let count = 0;
  let result = -1;

  function inorder(node: TreeNode | null) {
    if (!node || result !== -1) return;
    inorder(node.left);
    count++;
    if (count === k) {
      result = node.val;
      return;
    }
    inorder(node.right);
  }

  inorder(root);
  return result;
}
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Count nodes in inorder: 1, 2, 3, 4, 4
🚀 Application
expert
3:00remaining
Optimizing Kth Smallest Element Search with Node Counts
Given a BST where each node stores the count of nodes in its left subtree, how can you find the kth smallest element efficiently?
APerform a breadth-first search and count nodes until k is reached.
BAlways traverse the entire tree inorder and count nodes until k is reached.
CUse a stack to store all nodes and pop k times to get kth smallest.
DCompare k with left subtree count + 1 to decide to go left, right, or return current node.
Attempts:
2 left
💡 Hint
Use the stored counts to skip entire subtrees.