BST Find Maximum Element in DSA C# - Time & Space Complexity
We want to know how long it takes to find the biggest number in a Binary Search Tree (BST).
How does the time needed grow when the tree gets bigger?
Analyze the time complexity of the following code snippet.
public int FindMax(Node root) {
if (root == null) return int.MinValue;
Node current = root;
while (current.Right != null) {
current = current.Right;
}
return current.Value;
}
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 repeatedly.
- How many times: At most once per level down the right side of the tree.
As the tree grows taller on the right side, the steps to find the max grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if tree height is 4) |
| 100 | Up to 10 steps (if tree height is 10) |
| 1000 | Up to 15 steps (if tree height is 15) |
Pattern observation: The time depends on the height of the tree, not the total number of nodes.
Time Complexity: O(h)
This means the time grows with the height of the tree, which can be much smaller than the total nodes.
[X] Wrong: "Finding the max always takes time proportional to the number of nodes (n)."
[OK] Correct: The search only follows the right side path, so time depends on tree height, not total nodes.
Understanding how tree height affects search time helps you explain efficiency clearly and shows you know how data structure shape matters.
"What if the BST is balanced? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values and right child nodes have larger values compared to their parent.Step 2: Locate maximum element
The maximum element is the largest value, so it must be the rightmost node because values increase to the right.Final Answer:
At the rightmost node -> Option BQuick Check:
Maximum element = rightmost node [OK]
- Thinking maximum is at the root
- Confusing leftmost with maximum
- Assuming maximum can be any leaf
root?Solution
Step 1: Check traversal direction
To find max, traverse right child nodes until no more right child exists.Step 2: Verify code correctness
while (root.Right != null) { root = root.Right; } return root.Value; uses a while loop to move right untilroot.Rightis null, then returnsroot.Value, which is correct.Final Answer:
while (root.Right != null) { root = root.Right; } return root.Value; -> Option CQuick Check:
Traverse right until null, then return value [OK]
- Traversing left child instead of right
- Returning root value without traversal
- Using incorrect condition in loop
class Node {
public int Value;
public Node Left, Right;
public Node(int val) { Value = val; Left = Right = null; }
}
Node root = new Node(10);
root.Left = new Node(5);
root.Right = new Node(15);
root.Right.Right = new Node(20);
Node current = root;
while (current.Right != null) {
current = current.Right;
}
Console.WriteLine(current.Value);Solution
Step 1: Trace the BST structure
Root is 10, right child 15, right child of 15 is 20. So rightmost node is 20.Step 2: Follow the loop
The loop moves current from 10 to 15, then to 20, then stops because 20.Right is null.Final Answer:
20 -> Option DQuick Check:
Rightmost node value = 20 [OK]
- Stopping at 15 instead of 20
- Printing root value instead of max
- Confusing left and right children
int FindMax(Node root) {
if (root == null) return int.MinValue;
if (root.Right == null) return root.Value;
return FindMax(root.Left);
}Solution
Step 1: Analyze recursion direction
To find max, recursion must go right, but code recurses left incorrectly.Step 2: Identify correct recursion
ChangeFindMax(root.Left)toFindMax(root.Right)to find max correctly.Final Answer:
It should recurse on root.Right, not root.Left -> Option AQuick Check:
Max found by right recursion [OK]
- Recursing left child instead of right
- Returning wrong base case value
- Ignoring null root check
int FindMaxWithDuplicates(Node root) {
if (root == null) return int.MinValue;
int rightMax = FindMaxWithDuplicates(root.Right);
return Math.Max(root.Value, rightMax);
}
What is the output of FindMaxWithDuplicates on this BST?
Root: 10
Left: 5
Right: 10
Right.Right: 15
Right.Right.Right: 15Solution
Step 1: Understand duplicate handling
The method compares current node value with max from right subtree, so duplicates do not affect max finding.Step 2: Trace values
Right subtree max is 15 (rightmost node). Root value 10 is less than 15, so max is 15.Final Answer:
15 -> Option AQuick Check:
Max value with duplicates = 15 [OK]
- Assuming duplicates cause errors
- Returning first max found without comparison
- Ignoring right subtree values
Ready to prove it?
Pass the test and this pattern moves unknown
