A BST is a tree where each node has at most two children. For every node, all values in the left subtree are smaller, and all values in the right subtree are larger.
Click to reveal answer
beginner
How do you find the maximum element in a BST?
Start at the root and keep moving to the right child until there is no right child. The last node you reach is the maximum element.
Click to reveal answer
beginner
Why does moving right in a BST lead to the maximum element?
Because in a BST, right children always have values greater than their parents, so the farthest right node holds the largest value.
Click to reveal answer
intermediate
What is the time complexity of finding the maximum element in a BST?
The time complexity is O(h), where h is the height of the BST. In the worst case (skewed tree), it can be O(n).
Click to reveal answer
beginner
Show the Java code snippet to find the maximum element in a BST.
class Node {
int data;
Node left, right;
Node(int data) { this.data = data; }
}
int findMax(Node root) {
if (root == null) return Integer.MIN_VALUE;
Node current = root;
while (current.right != null) {
current = current.right;
}
return current.data;
}
Click to reveal answer
In a BST, where is the maximum element located?
AAt the leftmost node
BAt the rightmost node
CAt the root node
DAt any leaf node
✗ Incorrect
The maximum element is always at the rightmost node because right children have larger values.
What is the first step to find the maximum element in a BST?
AStart at the root
BMove to the right child
CCheck all nodes
DMove to the left child
✗ Incorrect
You start at the root and then move right repeatedly to find the maximum.
What happens if the BST is empty when finding the maximum element?
AReturn Integer.MIN_VALUE or an error
BReturn the root value
CReturn 0
DReturn null
✗ Incorrect
If the BST is empty, returning Integer.MIN_VALUE or signaling no maximum is appropriate.
What is the time complexity to find the maximum element in a balanced BST with n nodes?
AO(n)
BO(1)
CO(log n)
DO(n log n)
✗ Incorrect
In a balanced BST, height h is about log n, so finding max is O(log n).
Which of these is the correct way to find the maximum element in a BST?
ATraverse left until no left child
BReturn the root value
CTraverse both left and right subtrees fully
DTraverse right until no right child
✗ Incorrect
Moving right until no right child leads to the maximum element.
Explain how to find the maximum element in a Binary Search Tree and why this method works.
Think about the BST property of right children.
You got /5 concepts.
Write a simple Java method to find the maximum element in a BST and describe its time complexity.
Use a while loop to traverse right children.
You got /5 concepts.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At any leaf node
B. At the leftmost node
C. At the root node
D. At the rightmost node
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 the rightmost node because it has no larger right child.
Final Answer:
At the rightmost node -> Option D
Quick Check:
Maximum = rightmost node [OK]
Hint: Maximum is always the farthest right node in BST [OK]
Common Mistakes:
Confusing maximum with root node
Thinking maximum is at leftmost node
Assuming maximum can be any leaf
2. Which of the following Java method signatures correctly finds the maximum element in a BST node class named Node?
easy
A. public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; }
B. public int findMax(Node root) { return root.left.data; }
C. public int findMax(Node root) { if(root == null) return 0; return root.data; }
D. public int findMax(Node root) { while(root.left != null) root = root.left; return root.data; }
Solution
Step 1: Check traversal direction for maximum
Maximum is found by moving right until no right child exists.
Step 2: Verify method logic
public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } correctly loops while root.right is not null, then returns root.data.
Final Answer:
public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } -> Option A
Quick Check:
Traverse right to find max [OK]
Hint: Loop right child until null to find max [OK]
Common Mistakes:
Looping left child instead of right
Returning root data without traversal
Not checking for null root
3. What will be the output of the following Java code snippet?
class Node {
int data;
Node left, right;
Node(int d) { data = d; left = right = null; }
}
public class BST {
public static int findMax(Node root) {
while(root.right != null) root = root.right;
return root.data;
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(20);
root.right.right = new Node(30);
System.out.println(findMax(root));
}
}
medium
A. 20
B. 30
C. 10
D. 5
Solution
Step 1: Trace the BST structure
Root=10, left=5, right=20, right.right=30. Maximum is rightmost node 30.
Step 2: Follow findMax method
Loop moves from 10 to 20 to 30, then stops as 30.right is null, returns 30.
Final Answer:
30 -> Option B
Quick Check:
Rightmost node data = 30 [OK]
Hint: Follow right children to last node for max [OK]
Common Mistakes:
Returning root data without traversal
Confusing left and right children
Ignoring right.right node
4. Identify the error in the following Java method to find the maximum element in a BST and select the corrected version.