树与二叉树——计算二叉树的双分支结点个数

题目:

计算一棵给定二叉树的所有双分支结点个数。

递归:
思路:
  1. 定义一个参数 int ans = 0;
  2. 若当前结点有左孩子和右孩子则ans+1;
  3. 若存在左孩子 则左孩子执行 1,2步骤;若存在右孩子则右孩子执行1,2步骤;
测试用例:
ABD##E##DF##G##     结果:3
ABD###CE##F##       结果:2
代码实现:
#include <iostream>
using namespace std;
#define Max 20

typedef struct treenode{
    char data;
    struct treenode *lchild,*rchild;
}treenode,*tree;

void buildtree(tree &t){
    char ch;
    ch = getchar();
    if (ch=='#') t = NULL;
    else{
        t = (treenode *)malloc(sizeof(treenode));
        t->data = ch;
        t->lchild = NULL;
        t->rchild = NULL;
        buildtree(t->lchild);
        buildtree(t->rchild);
    }
    
}


int getdigui(tree t,int &ans){
    if(t->lchild!=NULL&&t->rchild!=NULL) ans++;
    if(t->lchild) getdigui(t->lchild,ans);
    if(t->rchild) getdigui(t->rchild,ans);

    return ans;
}

int main(){

    tree t;
    int ans;
    buildtree(t);
    getdigui(t,ans);
    cout<<ans;
return 0;
}
非递归:
思路:
  1. 辅助队列q(用来层序遍历二叉树), int ans =0(用于记录结果值);
  2. 若结点不为空,则结点入队
  3. 若队列不为空,则出队,并判断出队结点是否同时有左右孩子,若有则ans+1;
  4. 若左孩子存在则左孩子入队,若右孩子存在则右孩子入队。
  5. 重复2、3、4步骤;
测试用例:
ABD##E##DF##G##     结果:3
ABD###CE##F##       结果:2
代码示例:

#include <iostream>
using namespace std;
#define Max 20

typedef struct treenode{
    char data;
    struct treenode *lchild,*rchild;
}treenode,*tree;

void buildtree(tree &t){
    char ch;
    ch = getchar();
    if (ch=='#') t = NULL;
    else{
        t = (treenode *)malloc(sizeof(treenode));
        t->data = ch;
        t->lchild = NULL;
        t->rchild = NULL;
        buildtree(t->lchild);
        buildtree(t->rchild);
    }
    
}
struct squeue{
    struct treenode *data[Max];
    int f,r,tag;
};

bool isEmpty(squeue q){
    if(q.f == q.r&&q.tag==0){
        return true;
    }
    return false;
}

bool isFull(squeue q){
    if (q.f==q.r&& q.tag==1)
    {
        return true;
    }
    return false;
    
}
//入队
bool enter(squeue &q,tree t){
    if (isFull(q))
    {
        return false;
    }
    
    q.data[q.r] = t;
    q.r = (q.r+1)%Max;
    q.tag =1;
    
    return true;
}

//出队
bool outs(squeue &q,treenode *&p){
    if (isEmpty(q))
    {
       return false;
    }
    p = q.data[q.f];
    q.f = (q.f+1)%Max;
    q.tag = 0;
    return true;
    
    
}

int getTwoTree(tree t){
        int ans=0;
        squeue q;
        q.f = q.r = q.tag = 0;
        if (t==NULL) return 0;
        
        enter(q,t);
        treenode *p;
        while (q.f<q.r)
        {
            outs(q,p);
            if(p->lchild!=NULL&&p->rchild) ans++;

            if(p->lchild!=NULL) enter(q,p->lchild);
            if(p->rchild!=NULL) enter(q,p->rchild);
        }
        

        cout<<"双分支结点个数为:"<<ans<<endl;

        return ans;
        
}


int main(){

    tree t;
    buildtree(t);
    getTwoTree(t);
return 0;
}
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值