Algorithm


Problem Name: 501. Find Mode in Binary Search Tree

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (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]

 

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • -105 <= Node.val <= 105

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const findMode = function(root) {
  if(root == null) return []
  const hash = {}
  traverse(root, hash)
  const res = Object.entries(hash).sort((a, b) => b[1] - a[1])
  const result = [res[0][0]]
  for(let i = 1; i < res.length; i++) {
    if(res[i][1] === res[0][1]) result.push(res[i][0])
    else break
  }
  return result
};

function traverse(node, hash) {
  if(node === null) return
  hash[node.val] = Object.prototype.hasOwnProperty.call(hash, node.val) ? hash[node.val] + 1 : 1
  traverse(node.left, hash)
  traverse(node.right, hash>
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
root = [1,null,2,2]

Output

x
+
cmd
[2]

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        from collections import Counter
        def traverse(node):
            if node: dic[node.val]+=1; traverse(node.left); traverse(node.right)
        dic = collections.Counter()
        traverse(root)
        mx=max(dic.values(),default=0)
        return [k for k,v in dic.items() if v==mx]
Copy The Code & Try With Live Editor

Input

x
+
cmd
root = [1,null,2,2]

Output

x
+
cmd
[2]

#3 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _0501_FindModeInBinarySearchTree
    {
        private List < int> result = new List<int>();
        private int count, modelCount, prevValue;

        public int[] FindMode(TreeNode root)
        {
            InOrder(root);
            return result.ToArray();
        }

        private void InOrder(TreeNode node)
        {
            if (node == null) return;

            InOrder(node.left);

            if (prevValue != node.val) count = 1;
            else count++;

            if (count > modelCount)
            {
                result.Clear();
                result.Add(node.val);
                modelCount = count;
            }
            else if (count == modelCount)
                result.Add(node.val);

            prevValue = node.val;
            InOrder(node.right);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
root = [0]

Output

x
+
cmd
[0]
Advertisements

Demonstration


Previous
#500 Leetcode Keyboard Row Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#502 Leetcode IPO Solution in C, C++, Java, JavaScript, Python, C# Leetcode