📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the largest number stored in the tree.Think of the BST like a family tree where each node has a number. The right child always has a bigger number than its parent. So, the biggest number is the rightmost node.
🎯 Goal: Build a simple BST with given numbers, then write code to find and print the maximum number in the BST.
📋 What You'll Learn
Create a BST node class called TreeNode with value, left, and right properties
Create a BST by manually linking nodes with these exact values: 15, 10, 20, 8, 12, 17, 25
Write a function called findMax that finds the maximum value in the BST by moving right
Print the maximum value found in the BST
💡 Why This Matters
🌍 Real World
BSTs are used in databases and search engines to quickly find the largest or smallest values.
💼 Career
Understanding BSTs and how to find max/min values is important for software engineers working with data structures and algorithms.
Progress0 / 4 steps
1
Create BST nodes with exact values
Create a class called TreeNode with a constructor that takes a value number and sets left and right to null. Then create these nodes exactly: root with value 15, node10 with value 10, node20 with value 20, node8 with value 8, node12 with value 12, node17 with value 17, and node25 with value 25.
DSA Typescript
Hint
Remember to set left and right to null in the constructor.
2
Link nodes to form the BST
Link the nodes to form the BST exactly as follows: root.left = node10, root.right = node20, node10.left = node8, node10.right = node12, node20.left = node17, and node20.right = node25.
DSA Typescript
Hint
Remember, left child is smaller, right child is bigger.
3
Write function to find maximum value in BST
Write a function called findMax that takes a TreeNode called node and returns the maximum value in the BST. Use a while loop to move to the right child until it is null. Return the value of the rightmost node.
DSA Typescript
Hint
Keep moving right until there is no right child.
4
Print the maximum value in the BST
Use console.log to print the maximum value found by calling findMax(root).
DSA Typescript
Hint
Call findMax(root) and print the result using console.log.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At the rightmost node
B. At the leftmost node
C. At the root node
D. At any leaf 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 found by moving to the rightmost node because it has the largest value.
Final Answer:
At the rightmost node -> Option A
Quick Check:
Maximum element = rightmost node [OK]
Hint: Maximum is always the rightmost node in BST [OK]
Common Mistakes:
Thinking maximum is at the root
Confusing leftmost with maximum
Assuming maximum can be any leaf
2. Which TypeScript code snippet correctly finds the maximum element in a BST node?
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.
Hint: Traverse right child nodes until none left [OK]
Common Mistakes:
Traversing left instead of right
Using recursion incorrectly
Using Math.max on subtree values (not BST property)
3. What will be the output of the following TypeScript code snippet?
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));
medium
A. 10
B. 5
C. 15
D. 20
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 D
Quick Check:
Rightmost node value = 20 [OK]
Hint: Rightmost node value is maximum [OK]
Common Mistakes:
Returning root value instead of rightmost
Confusing left and right children
Not following right child chain fully
4. The following TypeScript function is intended to find the maximum element in a BST. What is the error?
function findMax(node: Node): number {
while(node.left !== null) {
node = node.left;
}
return node.value;
}
medium
A. It returns undefined because node.value is missing
B. It causes infinite loop if node.left is null
C. It traverses left instead of right, so returns minimum instead of maximum
D. It throws error because Node type is incorrect
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 C
Quick Check:
Left traversal = minimum, not maximum [OK]
Hint: Traverse right for max, left for min [OK]
Common Mistakes:
Confusing left and right child traversal
Assuming left traversal finds maximum
Ignoring BST property of ordering
5. Given a BST where some nodes have only left children, how would you modify the findMax function to correctly find the maximum element even if the tree is skewed left?
hard
A. Return the root node's value directly since left skew means root is max
B. Traverse right child nodes until null; if none, return current node's value
C. Traverse left child nodes until null and return that value
D. Use recursion: return Math.max(node.value, findMax(node.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 B
Quick Check:
Right traversal or current node if no right child [OK]
Hint: Check right child; if none, current node is max [OK]
Common Mistakes:
Assuming root is always max in left skew
Traversing left to find max
Using recursion unnecessarily for simple right traversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.