#include <iostream>
using std::cin;
using std::cout;
using std::endl;
typedef struct BiTNode {
char data;
struct BiTNode *Lchild, *Rchild; // 左、右孩子指针
} *BiTree;
void CreateBiTree(BiTree &T){
// 字符'#'表示空树,其它字母字符为结点的数据元素
char ch;
cin >> ch ;
if (ch=='#'){
T=NULL; // 建空树
}else {
T = new BiTNode ; // "访问"操作为生成根结点
T->data = ch;
CreateBiTree(T->Lchild); // 递归建(遍历)左子树
CreateBiTree(T->Rchild); // 递归建(遍历)右子树
}//else
}//CreateBiTree
//先序遍历以T为根指针的二叉树
void PreOrder(BiTree &T){
if(T){ // T=NULL时,二叉树为空树,不做任何操作
cout<< T->data << " "; // 通过函数指针 *visit 访问根结点
PreOrder(T->Lchild);
PreOrder(T->Rchild);
}// if
}
//中序遍历以T为根指针的二叉树
void InOrder(BiTree &T){
if(T){ //
InOrder(T->Lchild); //
cout<< T->data << " "; //
InOrder(T->Rchild); //
}// if
}
//后序遍历以T为根指针的二叉树
void PostOrder(BiTree &T){
if(T){ // T=NULL时
PostOrder(T->Lchild); //
PostOrder(T->Rchild); //
cout<< T->data << " "; //
}//
}
// 先序遍历二叉树,以 count 返回二叉树中叶子结点的数目
void CountLeaf(BiTree &T, int &count){
if (T) {
if ((!T->Lchild)&& (!T->Rchild))
count++; // 对叶子结点计数
CountLeaf( T->Lchild, count);
CountLeaf( T->Rchild, count);
} // if
} // CountLeaf
// T指向二叉树的根,level 为 T 所指结点所在层次,
// 其初值为1,depth 为当前求得的最大层次,其初值为0
void BiTreeDepth(BiTree T, int level, int &depth){
if (T){
if (level>depth) depth=level;
BiTreeDepth(T->Lchild, level+1, depth);
BiTreeDepth(T->Rchild, level+1, depth);
}// if
}// BiTreeDepth
温馨提示:答案为网友推荐,仅供参考