统计利用先序遍历创建的二叉树的深度
输入为先序遍历二叉树结点序列。
对应的二叉树的深度。
A## ABC#### AB##C## ABCD###E#F##G## A##B##
1 3 2 4 1
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cstdio> 6 typedef int Datetype; 7 using namespace std; 8 typedef struct link{ 9 Datetype date; 10 struct link *lchild; 11 struct link *rchild; 12 }tree; 13 14 void creattree(tree *&L) 15 { 16 char c; 17 cin>>c; 18 if(c==‘#‘) 19 L=NULL; 20 else 21 { 22 L = (tree *)malloc(sizeof(tree)) ; 23 L->date=c; 24 creattree(L->lchild); 25 creattree(L->rchild); 26 } 27 } 28 29 void destroytree(tree *&L) 30 { 31 if(L!=NULL) 32 { 33 destroytree(L->lchild); 34 destroytree(L->rchild); 35 free(L); 36 } 37 } 38 39 int deep(tree *L) 40 { 41 int ldep,rdep,max; 42 if(L!=NULL) 43 { 44 ldep=deep(L->lchild); 45 rdep=deep(L->rchild); 46 max=ldep>rdep?ldep+1:rdep+1; 47 return max; 48 } 49 else 50 return 0; 51 } 52 53 int main() 54 { 55 tree *L = NULL; 56 int x; 57 creattree(L); 58 x=deep(L); 59 cout<<x; 60 destroytree(L); 61 return 0; 62 }
原文:https://www.cnblogs.com/Iwpml-595/p/10691521.html