一、输出二叉树中的结点
typedef struct Node {
DataType data;
struct Node *LChild;
struct Node *RChild;
} BiTNode,*BiTree;
viod PreOrder(BiTree root) {
if(root!=NULL) {
printf(root->data); /*先序遍历输出根结点 */
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
二、输出二叉树中的叶子结点
void PreOrder(BiTree root) {
if(root!=NULL) {
if(root->LChild==NULL&&root->RChild==NULL)
Printf(root->data); /*先序遍历输出叶子结点 */
PreOrder(root->LChild);
PreOrder(root->RChild);
}
}
三、统计叶子结点的数目
1 后序遍历
void Leaf(BiTree root) {
int LeafCount;
if(root!=NULL) {
Leaf(root->LChild);
Leaf(root->RChild);
if(root->LChild==NULL&&root->RChild==NULL)
LeafCounti++;
}
}
2 后序遍历
void Leaf(BiTree root) {
int LeafCount;
if(root!=NULL) {
Leaf(root->LChild);
Leaf(root->RChild);
if(root->LChild==NULL&&root->RChild==NULL)
LeafCount++;
}
}
3
void Leaf(BiTree root) {
int LeafCount;
if(root!=NULL)
LeafCount=0;
else if(root->LChild==NULL&&root->RChild==NULL)
LeafCount=1;
else
LeafCount=leaf(root->LChild)+leaf(root->RChild);//左右子树叶子数目之和
return Leaf(root->RChild);
}
}
四、后序遍历发求二叉树的高度
int PostTreeDepth(BiTree bt) {
int hl,hr,max;
if(bt!=NULL) {
hl=PostTreeDepth(bt->LChild); //求左子树深度
hr=PostTreeDepth(bt->RChild);//求右子树深度
max=hl>hr?hl:hr;
return(max+1); //返回树的深度
} else
return 0;
}