Simple O(n) Python solution, using dictionary(extra space), max and list comprehension.

  • 0
    class Solution(object):
        def occNum(self, root, dic):
            if root is None: return
            if not root.val in dic:
                dic[root.val] = 1
                dic[root.val] += 1
            self.occNum(root.left, dic)
            self.occNum(root.right, dic)
        def findMode(self, root):
            if root is None: return []
            dic = {}
            self.occNum(root, dic)
            max_c = max(dic.values())
            return [e for (e,c) in dic.items() if c==max_c]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.