What if you could find the biggest number instantly without checking every single one?
Why BST Find Maximum Element in DSA Typescript?
Imagine you have a big stack of books sorted by height, but they are all mixed up on a table. You want to find the tallest book quickly.
If you try to check each book one by one, it will take a long time and you might lose track.
Checking every book manually is slow and tiring. You might miss some books or get confused about which is tallest.
Doing this with many numbers or data points by hand is even harder and error-prone.
A Binary Search Tree (BST) organizes data so that the biggest item is always easy to find by just following one path.
Finding the maximum means going right until you can't go further, making it fast and simple.
function findMaxManual(arr: number[]): number {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}function findMaxBST(root: TreeNode | null): number | null {
if (!root) return null;
let current = root;
while (current.right) {
current = current.right;
}
return current.value;
}This lets you quickly find the largest value in a sorted structure without checking every item.
In a leaderboard for a game, finding the highest score fast is important. A BST helps find the top score quickly.
Manual search is slow and error-prone.
BST structure makes max value easy to find by going right.
Efficient for large sorted data sets.
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes are smaller and right child nodes are larger than the parent node.Step 2: Locate maximum element
The maximum element is found by moving to the rightmost node because it has the largest value.Final Answer:
At the rightmost node -> Option AQuick Check:
Maximum element = rightmost node [OK]
- Thinking maximum is at the root
- Confusing leftmost with maximum
- Assuming maximum can be any leaf
Solution
Step 1: Identify correct traversal direction
To find maximum in BST, traverse right child nodes until no more right child exists.Step 2: Check code correctness
while(node.right !== null) { node = node.right; } return node.value; uses a while loop to move right until right child is null, then returns node value.Final Answer:
while(node.right !== null) { node = node.right; } return node.value; -> Option AQuick Check:
Traverse right until null, then return value [OK]
- Traversing left instead of right
- Using recursion incorrectly
- Using Math.max on subtree values (not BST property)
class Node {
constructor(public value: number, public left: Node | null = null, public right: Node | null = null) {}
}
function findMax(node: Node): number {
while(node.right !== null) {
node = node.right;
}
return node.value;
}
const root = new Node(10, new Node(5), new Node(15, null, new Node(20)));
console.log(findMax(root));Solution
Step 1: Understand tree structure
Root is 10, left child 5, right child 15, and 15's right child is 20.Step 2: Trace findMax function
Function moves right from 10 to 15, then to 20, which has no right child, so returns 20.Final Answer:
20 -> Option DQuick Check:
Rightmost node value = 20 [OK]
- Returning root value instead of rightmost
- Confusing left and right children
- Not following right child chain fully
function findMax(node: Node): number {
while(node.left !== null) {
node = node.left;
}
return node.value;
}Solution
Step 1: Analyze traversal direction
The function moves left child nodes, which leads to the minimum element in BST.Step 2: Identify correct traversal for maximum
Maximum element requires moving right child nodes, not left.Final Answer:
It traverses left instead of right, so returns minimum instead of maximum -> Option CQuick Check:
Left traversal = minimum, not maximum [OK]
- Confusing left and right child traversal
- Assuming left traversal finds maximum
- Ignoring BST property of ordering
findMax function to correctly find the maximum element even if the tree is skewed left?Solution
Step 1: Understand left skewed BST
In a BST, values in left subtree < node <= values in right subtree. Even if some nodes have no right children, the maximum is found by traversing right until null.Step 2: Modify findMax logic
Traverse right child nodes until null; if no right child exists, current node is max.Final Answer:
Traverse right child nodes until null; if none, return current node's value -> Option BQuick Check:
Right traversal or current node if no right child [OK]
- Assuming root is always max in left skew
- Traversing left to find max
- Using recursion unnecessarily for simple right traversal
Ready to prove it?
Pass the test and this pattern moves unknown
