#include <iostream>
#include <cstring>
#include <cstdlib> //包含malloc 函数
using namespace std;
typedef char ElemType; //定义节点的类型
typedef struct BitNode{ //特殊的双链表,包含数据元素、左指针、右指针
ElemType data;
struct BitNode *lchild,*rchild;
}*BitTree,BitNode;
void InPutElem(ElemType &x){
cin >> x;
}
int CreateBitTree(BitTree &T){//创建并返回深度
char Elem;
int l=0,r=0;
InPutElem(Elem);
if(Elem=='#'){
T = NULL;
return 0; //空节点不增加深度
}else{
T = (BitTree)malloc(sizeof(BitNode));
T->data = Elem; //前序遍历
l = CreateBitTree(T->lchild);
r = CreateBitTree(T->rchild);
// cout << l << r <<endl;
return l>r ? (l+1):(r+1); //返回最大的一个
}
}
int main(){
BitTree T = NULL;
T = (BitTree)malloc(sizeof(BitNode));
cout << CreateBitTree(T);
return 0;
}
SWUST OJ 971: 统计利用先序遍历创建的二叉树的深度
于 2022-04-23 12:46:38 首次发布