BST Find Maximum Element in DSA Typescript - Time & Space Complexity
Finding the maximum element in a Binary Search Tree (BST) involves moving through the tree nodes.
We want to know how the time to find this maximum grows as the tree gets bigger.
Analyze the time complexity of the following code snippet.
function findMax(root: TreeNode | null): number | null {
if (root === null) return null;
let current = root;
while (current.right !== null) {
current = current.right;
}
return current.val;
}
This code finds the maximum value by moving right until no more right child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child in a loop.
- How many times: At most once per level down the right side of the tree.
Each step moves one node to the right until the last node is reached.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps if tree is skewed right |
| 100 | Up to 100 steps in worst case |
| 1000 | Up to 1000 steps in worst case |
Pattern observation: The number of steps grows linearly with the height of the tree's right side.
Time Complexity: O(h)
This means the time depends on the height of the tree, moving down the right side nodes.
[X] Wrong: "Finding max is always O(log n) because BSTs are balanced."
[OK] Correct: Not all BSTs are balanced; if the tree is skewed, height can be as large as n, making it slower.
Understanding how tree shape affects search time helps you explain and improve tree operations in real projects.
"What if the BST was balanced? How would the time complexity change when finding the maximum element?"
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
