LeetcodeJun 08, 2025

Find Mode in Binary Search Tree

Hazrat Ali

Leetcode

Given the root of a binary search tree (BST) with duplicates, return all the modes (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

 

Example 1:

Input: root = [1,null,2,2]
Output: [2]

Example 2:

Input: root = [0]
Output: [0]


Solution
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
const findMode = root => {
 
  const handleValue = val => {
    if (val !== currVal) {
      currVal = val;
      currCount = 0;
    }
    currCount++;

    if (currCount > maxCount) {
     
      maxCount = currCount;
      modeCount = 1;
      modes[0] = currVal;
    } else if (currCount === maxCount) {
     
      modes[modeCount] = currVal;
      modeCount++;
    }
  };

  const inorder = node => {
    if (!node) {
      return;
    }

    inorder(node.left);
    handleValue(node.val);
    inorder(node.right);
  };

  let currVal = null;
  let currCount = 0;
  let maxCount = 0;
  let modeCount = 0;

  const modes = [];

  inorder(root);

  return modes.slice(0, modeCount);
};


Comments