0
0
DSA Typescriptprogramming~10 mins

BST Find Maximum Element in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the maximum value in a BST by moving right.

DSA Typescript
function findMax(root: TreeNode | null): number | null {
  if (root === null) return null;
  let current = root;
  while (current.[1] !== null) {
    current = current.right;
  }
  return current.val;
}
Drag options to blanks, or click blank then click option'
Aleft
Bparent
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' to traverse.
Returning root value without traversal.
2fill in blank
medium

Complete the code to recursively find the maximum value in a BST.

DSA Typescript
function findMaxRecursive(root: TreeNode | null): number | null {
  if (root === null) return null;
  if (root.[1] === null) return root.val;
  return findMaxRecursive(root.right);
}
Drag options to blanks, or click blank then click option'
Aright
Bparent
Cleft
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Checking left child instead of right.
Not handling null root case.
3fill in blank
hard

Fix the error in the code to correctly find the maximum value in a BST iteratively.

DSA Typescript
function findMaxIterative(root: TreeNode | null): number | null {
  if (root === null) return null;
  let current = root;
  while (current.[1] !== null) {
    current = current.[1];
  }
  return current.val;
}
Drag options to blanks, or click blank then click option'
Achild
Bleft
Cparent
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Moving left instead of right in the loop.
Returning wrong node value.
4fill in blank
hard

Fill both blanks to create a function that finds the maximum value in a BST using recursion.

DSA Typescript
function findMax(root: TreeNode | null): number | null {
  if (root === null) return null;
  if (root.[1] === null) return root.val;
  return findMax(root.[2]);
}
Drag options to blanks, or click blank then click option'
Aright
Bleft
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using left child in conditions.
Not returning the recursive call properly.
5fill in blank
hard

Fill all three blanks to implement an iterative function that finds the maximum value in a BST.

DSA Typescript
function findMax(root: TreeNode | null): number | null {
  if (root === null) return null;
  let [1] = root;
  while ([2].[3] !== null) {
    [2] = [2].right;
  }
  return current.val;
}
Drag options to blanks, or click blank then click option'
Acurrent
Broot
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' instead of a traversal variable.
Moving left instead of right.