1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int numColor(TreeNode root) { Set<Integer> colors = new HashSet<>(); dfs(colors, root); return colors.size(); }
private void dfs(Set<Integer> colors, TreeNode root) { if (root == null) { return; }
colors.add(root.val); dfs(colors, root.left); dfs(colors, root.right); } }
|