0
0
DSA Typescriptprogramming

BST Find Maximum Element in DSA Typescript

Choose your learning style9 modes available
Mental Model
In a Binary Search Tree, the biggest number is always found by going as far right as possible.
Analogy: Imagine a family tree where older generations are on the left and younger generations on the right. The youngest person is the one farthest to the right.
      10
     /  \
    5    15
         /  \
        12   20
               
               ↑ (maximum)
Dry Run Walkthrough
Input: BST with nodes: 10 -> 5, 15 -> 12, 20; find maximum element
Goal: Find the largest value in the BST by moving right until no more right child exists
Step 1: Start at root node 10
10 -> 5 (left), 15 (right) ↑
Why: We begin at the root to find the maximum
Step 2: Move right from 10 to 15
15 -> 12 (left), 20 (right) ↑
Why: Maximum is always in the right subtree
Step 3: Move right from 15 to 20
20 ↑
Why: Keep moving right to find the largest value
Step 4: No right child of 20, stop here
20 ↑
Why: Reached the rightmost node, which is the maximum
Result:
20
Annotated Code
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 findMax(root: TreeNode | null): number | null {
  if (root === null) return null; // empty tree
  let current = root;
  while (current.right !== null) {
    current = current.right; // move right to find max
  }
  return current.val; // rightmost node value
}

// Driver code
const root = new TreeNode(10,
  new TreeNode(5),
  new TreeNode(15, new TreeNode(12), new TreeNode(20))
);
const maxVal = findMax(root);
console.log(maxVal !== null ? maxVal.toString() : "Tree is empty");
if (root === null) return null; // empty tree
handle empty tree edge case
while (current.right !== null) {
keep moving right to find the maximum value
current = current.right; // move right to find max
advance current pointer to right child
return current.val; // rightmost node value
return the value of the rightmost node
OutputSuccess
20
Complexity Analysis
Time: O(h) because we traverse down the height of the tree following right children only
Space: O(1) because we use only a few variables and no extra data structures
vs Alternative: Compared to searching all nodes (O(n)), this is faster since max is always rightmost, so we skip left subtrees
Edge Cases
Empty tree (root is null)
Returns null indicating no maximum value
DSA Typescript
if (root === null) return null; // empty tree
Tree with only one node
Returns the single node's value as maximum
DSA Typescript
while (current.right !== null) {
Tree where all nodes are left children (no right child)
Returns root value as maximum since no right child exists
DSA Typescript
while (current.right !== null) {
When to Use This Pattern
When you need the largest value in a BST, look for the rightmost node by following right children until none remain.
Common Mistakes
Mistake: Trying to check left children or searching entire tree for max
Fix: Only follow right children because BST property guarantees max is rightmost
Summary
Finds the maximum value in a BST by moving right until no right child exists.
Use when you want the largest element quickly in a BST.
The maximum is always the rightmost node in a BST.