Recall & Review
beginner
What is a Binary Search Tree (BST)?
A BST is a tree where each node has at most two children. The left child's value is less than the node's value, and the right child's value is greater. This helps in fast searching.
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, all right children have values greater than their parents, so the rightmost node holds the largest value.
Click to reveal answer
intermediate
What is the time complexity to find the maximum element in a BST?
The time complexity is O(h), where h is the height of the tree. In the worst case (skewed tree), it can be O(n), and in a balanced tree, it is O(log n).
Click to reveal answer
intermediate
Show the TypeScript code snippet to find the maximum element in a BST.
function findMax(root: TreeNode | null): number | null {
if (!root) return null;
let current = root;
while (current.right) {
current = current.right;
}
return current.val;
}Click to reveal answer
In a BST, where is the maximum element located?
✗ Incorrect
The maximum element is always the rightmost node in a BST because right children have greater values.
What is the first step to find the maximum element in a BST?
✗ Incorrect
You start at the root and move to the right child repeatedly to find the maximum.
What is the time complexity to find the maximum element in a balanced BST?
✗ Incorrect
In a balanced BST, height is log n, so finding max takes O(log n) time.
If a BST has only one node, what is the maximum element?
✗ Incorrect
With one node, that node is both minimum and maximum.
Which of these is NOT true about finding the maximum in a BST?
✗ Incorrect
You do not need to check all nodes; just follow the right children.
Explain step-by-step how to find the maximum element in a BST.
Think about the BST property of right children.
You got /4 concepts.
Write a simple TypeScript function to find the maximum element in a BST and explain how it works.
Use a while loop to traverse right children.
You got /4 concepts.