blob: 550da04504cbaa6aa77adb5f504b33cab137945b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class Solution {
public:
int visited[1001] {0};
int result = 0;
void DFS(TreeNode* r) {
if (!visited[r->val]) {
result += 1;
visited[r->val] = 1;
}
if (r->left) DFS(r->left);
if (r->right) DFS(r->right);
}
int numColor(TreeNode* root) {
DFS(root);
return result;
}
};
|