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 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; 
}

 

posted @ 2018-10-16 16:07  有梦就要去实现他  阅读(293)  评论(0编辑  收藏  举报