self.dict[node.val] = 1 + self.dict.get(node.val, 0) self.max = self.max if self.dict[ node.val] < self.max else self.dict[node.val] if node.left: dfs(node.left) if node.right: dfs(node.right) if root: dfs(root) # return [k for k in self.dict.keys() if self.dict[k] == self.max ] # # additional list space without list comprehension # 39% ans = [] for k, v in self.dict.items(): if v == self.max: ans.append(k) return ans if __name__ == '__main__': s = Solution() # tc = [[1,None,2,2]] #tc = [[1,None,2,None,None,2,None]] tc = [[1, 2, 3, 4, 5, 6, 3]] an = [[2]] import helper t = helper.TestHelper() for i in range(len(tc)): c = t.listToBinaryTree(tc[i]) print(s.findMode(c)) #assert(s.findMode(c) == an[i])
def setUp(self): self.h = helper.TestHelper() self.assertNotEqual(None, self.h.driver)