//判断一棵树是不是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 0;
    int count = 0;
    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; 
}

 

相关文章:

  • 2021-04-10
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
  • 2021-07-05
  • 2021-11-23
  • 2022-01-21
猜你喜欢
  • 2021-07-06
  • 2021-12-07
  • 2021-08-16
  • 2022-12-23
  • 2021-12-18
  • 2021-09-28
  • 2021-08-12
相关资源
相似解决方案