Leetcode Problem 501. Find Mode in Binary Search Tree

501. Find Mode in Binary Search Tree

Leetcode Solutions

Inorder Traversal with Constant Space

  1. Initialize global variables to keep track of the current streak (currStreak), the value of the current streak (currNum), and the maximum streak (maxStreak).
  2. Initialize an empty list result to store the modes.
  3. Perform an inorder traversal of the BST. For each node: a. If the node's value is the same as currNum, increment currStreak. b. If the node's value is different, compare currStreak with maxStreak and update result accordingly. Reset currStreak to 1 and currNum to the node's value. c. If currStreak is greater than maxStreak, update maxStreak and reset result to only contain the current node's value. d. If currStreak equals maxStreak, append the current node's value to result.
  4. After the traversal, return result as the list of modes.
UML Thumbnail

Count Frequency with Hash Map (DFS)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...