日野弥生:勉強しよう
LeetCode LCR 44 - 开幕式焰火
发表于2025年03月13日
哈希表记录颜色, 最后
class Solution:
def numColor(self, root: TreeNode) -> int:
# 哈希表记录颜色
hash = set()
def numColorIMPL(root):
nonlocal hash
if not root:
return
if root.val not in hash:
hash.add(root.val)
numColorIMPL(root.left)
numColorIMPL(root.right)
numColorIMPL(root)
return len(hash)