0
0
DSA Typescriptprogramming~5 mins

BST Find Maximum Element in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ARightmost node
BLeftmost node
CRoot node
DAny leaf node
What is the first step to find the maximum element in a BST?
AMove to the left child
BMove to the right child
CCheck the root value
DTraverse all nodes
What is the time complexity to find the maximum element in a balanced BST?
AO(n)
BO(1)
CO(log n)
DO(n log n)
If a BST has only one node, what is the maximum element?
AThe root node's value
BNull
CThe left child's value
DThe right child's value
Which of these is NOT true about finding the maximum in a BST?
AYou always move right to find the max
BThe max is the rightmost node
CThe max is greater than all other nodes
DYou must check all nodes to find the max
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.