LCP 44.开幕式焰火

​​题目来源:

leetcode题目,网址:LCP 44. 开幕式焰火 - 力扣(LeetCode)

解题思路:

遍历并计数即可。

解题代码:

复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int numColor(TreeNode root) {
        Set<Integer> set=new HashSet<>();
        count(root,set);
        return set.size();
    }
    public void count(TreeNode root, Set<Integer> set){
        if(root==null){
            return ;
        }
        set.add(root.val);
        count(root.left,set);
        count(root.right,set);
    }
}
复制代码

总结:

无官方题解。


相关推荐
怒放吧德德4 小时前
Spring Boot 实战:RSA+AES 接口全链路加解密(防篡改 / 防重放)
java·spring boot·后端
郑州光合科技余经理8 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
大大水瓶9 小时前
Tomcat
java·tomcat
dustcell.9 小时前
haproxy七层代理
java·开发语言·前端
游离态指针9 小时前
以为发消息=下单成功?RabbitMQ从0到秒杀实战的完整踩坑笔记
java
琢磨先生David9 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
BD_Marathon9 小时前
工厂方法模式
android·java·工厂方法模式
玹外之音9 小时前
Spring AI MCP 无状态服务器实战:构建AI智能用户管理系统
java·后端
java干货9 小时前
Redis 的 ZipList 是什么?它是怎么解决内存碎片问题的?
java
失重外太空啦9 小时前
Tomcat
java·服务器·tomcat