Two input modes are designed to create a binary tree. The depth of the binary tree is output. The leaf nodes of the binary tree are output.
# Include <stdio. h>
# Include <malloc. h>
Struct Node {
Char Data;
Struct Node * lchild;
Struct Node * rchild;
};
Node * creat (node * P)
{
Char Ch;
Scanf ( " % C " , & Ch );
If (CH = ' , ' )
P = NULL;
Else
{
P = (node *) malloc (Sizeof (Node ));
P-> DATA = CH;
P-> lchild = creat (p-> lchild );
P-> rchild = creat (p-> rchild );
}
Return P;
}
VoidTravel_zhongxu (node * P)
{
If(P! = NULL ){
Travel_zhongxu (p-> lchild );
Printf ("% C", P-> data );
Travel_zhongxu (p-> rchild );
}
}
Void Travel_houxu (node * P)
{
If (P! = NULL ){
Travel_houxu (p-> lchild );
Travel_houxu (p-> rchild );
Printf ( " % C " , P-> data );
}
}
Void Leaf (node * P, Int & L)
{
If (P & P-> lchild = NULL & P-> rchild = NULL)
L ++;
If (P! = NULL)
{
Leaf (p-> lchild, L );
Leaf (p-> rchild, L );
}
}
IntDeepth (node * P)
{
IntD, DL, Dr;
If(! P)
D =0;
Else{
DL = deepth (p-> lchild );
Dr = deepth (p-> rchild );
D =1+ (DL> Dr? DL: DR );
}
ReturnD;
}
Int Main ()
{
Node * tree;
Tree = creat (tree );
Travel_zhongxu (tree );
Printf ( " \ N " );
Travel_houxu (tree );
Printf ( " \ N " );
Int L = 0 ;
Leaf (tree, L );
Printf ( " % D \ n " , L );
Int Deep;
Deep = deepth (tree );
Printf ( " % D \ n " , Deep );
}