日野弥生:勉強しよう

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)

フラッシュタブ:LeetCode

题目链接:https://leetcode.cn/problems/sZ59z6/

上一篇

LeetCode 872 - 叶子相似的树

下一篇

LeetCode LCR 150 - 彩灯装饰记录 II