【例】遍历二叉树的应用:输出二叉树中的叶子结点。
typedef struct TreeNode *BinTree;typedef BinTree Position; struct TreeNode{ElementType Data;BinTree Left;BinTree Right; }; BinTree BT;
void PReOrderPrintLeaves(BinTree BT)//例如先序遍历来判断左右子树是否都为空 {if(BT){if(!BT->Left&&!BT->Right)//增加检测结点 printf("%d",BT->Data);PreOrderPrintLeaves(BT->Left);PreOrderPrintLeaves(BT->Right);}}
【例】求二叉树的高度。
int PostOrderGetHeight(BinTree BT) { int HL;HR,MaxH;if(BT){HL=PostOrderGetHeight(BT->Left);//求左子树的深度HR=PostOrderGetHeight(BT->Right);//求右子树的深度 MaxH=(HL>HR)?HL:HR;//取左右子树较大的深度return (MaxH+1);//返回树的深度 } else return 0;//空树深度为0 }
【例】二元运算表达式树及其遍历
【例】由两种遍历序列确定二叉树
新闻热点
疑难解答