Find mode in binary search tree

November 15, 2022

tree

Problem URL: Find mode in binary search tree

We will use a hashmap to store the frequency of each node's value. We will then traverse the hashmap and return the keys with the maximum frequency.

class Solution:
    def findMode(self, root: TreeNode) -> List[int]:
        freq = {}
        def dfs(node: TreeNode):
            if not node:
                return
            freq[node.val] = freq.get(node.val, 0) + 1
            dfs(node.left)
            dfs(node.right)

        dfs(root)
        maxFreq = max(freq.values())
        return [k for k, v in freq.items() if v == maxFreq]

Time complexity: O(n)
Space complexity: O(n)