#include <stdio.h> #include <string.h> typedef struct LNode { //定义树结点类型; char date; LNode *left;LNode *right; }LNode, *LinkList; LinkList L,LL;//LL为栈指针; void Print1(LinkList L) { //递归中序遍历二叉树; if(L) { Print1(L->left); printf("%d ",L->date); Print1(L->right); } } void Pop(LinkList L,LinkList &e) { //入栈操作; } void Push(LinkList L) { //出栈操作; } void Print2(LinkList L) { //非递归的方法中序遍历二叉树; //需要利用栈; LinkList p = L; LinkList q = new LNode;//这里重新定义一个结点,是为了保存从栈中取出的结点值,防止改变原结点值; while(p) { if(p) { Push(p); p=p->left; } else { Pop(LL,q); printf("%d ",q->date); p=q->right; } } } void Creat(LinkList &L) { //先序的递归方式建立二叉树; char ch;scanf("%c",&ch); if(ch!='#') { L = NULL;//表明当前结点指向空;当前结点即为其父节点的左或右结点; } else { L = new LNode;//L先创建结点,之后为其赋值,然后递归创建其左右子树; L->date = ch; Creat(L->left); Creat(L->right); } } void Copy(LinkList L,LinkList &LL) { //递归复制二叉树到另外一个数LL中; if(!L) LL = NULL; else { LL = new LNode; LL = L; Copy(L->left,LL->left); Copy(L->right,LL->right); } } int Depth(LinkList L) { //思想:递归计算其左子树和右子树的深度,返回max+1; if(L == NULL) return 0; int n,m; n=Depth(L->left); m=Depth(L->right); return m>n ? (m+1):(n+1) ; } int NodeCount(LinkList L) { //递归统计树中全部结点的个数; if(!L) return 0; else return (NodeCount(L->left)+NodeCount(L->right)+1); } int main() { return 0; }