uniform_tree以及其变体

时间:2023-03-09 12:57:19
uniform_tree以及其变体
//判断一棵树是不是uniform-tree
bool uniform_tree(TreeNode* root){
if(root == NULL)
return true;
return uniform_core(root,root->val);
} bool uniform_core(TreeNode* root,int value){
if(root == NULL)
return true;
if(root->val != value)
return false;
bool left = uniform_core(root->left,value);
bool right = uniform_core(root->right,value);
return left && right;
} //判断有多少个子树是uniform-tree
int uniform_tree(TreeNode* root){
if(root == NULL)
return ;
int count = ;
bool flag = uniform_core(root->left,root->val,count) && uniform_core(root->right,root->val,count);
if(flag)
count++;
return count;
} bool uniform_core(TreeNode* root,int value,int& count){
if(root == NULL)
return true;
bool flag1 = uniform_core(root->left,root->val,count) && uniform_core(root->right,root->val,count);
if(flag1)
count++;
bool flag2 = (root->val == value);
return flag1 && flag2;
}