目录
一、题目
二、代码
一、题目
二、代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
static int MAX;
void MAX_ProOrder(TreeNode* root)
{
if(root==nullptr)//结束条件
{
return;
}
MAX = MAX>root->val?MAX:root->val;
MAX_ProOrder(root->left);
MAX_ProOrder(root->right);
}
void ProOrder(TreeNode* root,vector<int>&a)
{
if(root==nullptr)
{
return;
}
a[root->val]=1;
ProOrder(root->left,a);
ProOrder(root->right,a);
}
int numColor(TreeNode* root) {
MAX_ProOrder(root);
vector<int>a(MAX+1,0);
ProOrder(root,a);
int Count=0;
for(int i=0;i<=MAX;i++)
{
if(a[i]!=0)
{
Count++;
}
}
return Count;
}
};
int Solution::MAX=0;