文章

11

粉丝

47

获赞

3

访问

2.6k

头像
二叉树的深度 题解:
P4356
发布于2024年3月9日 13:58
阅读数 401

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;

typedef struct BiNode{
    char data;
    BiNode *lchild,*rchild;
}BiNode,*BiTree;

//先序遍历创建二叉树
BiTree CreatBitree(){
    BiTree T;//创建一个遍历指针
    char c;
    cin>>c;
    if(c=='#'){
        T=NULL;//使当前指针为空
    }else{
        T=(BiTree)malloc(sizeof(BiNode));//分配空间
        T->data=c;
        T->lchild=CreatBitree();
        T->rchild=CreatBitree();
    }
    return T;
}

//获取树的深度
int getdepth(BiTree T){
    if(T==NULL){
        return 0;
    }else{
        return 1+max(getdepth(T->lchild),getdepth(T->rchild));
    }
}

int main() {
    BiTree T;
    T=C...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发