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 set=new HashSet<>();
        count(root,set);
        return set.size();
    }
    public void count(TreeNode root, Set set){
        if(root==null){
            return ;
        }
        set.add(root.val);
        count(root.left,set);
        count(root.right,set);
    }
}

总结:

        无官方题解。


你可能感兴趣的:(#,Java,LeetCode,Java)