MOOC 3.2 二叉树的遍历应用
// 遍历二叉树的应用 // 输出二叉树中的叶子结点 // 在二叉树的遍历算法中增加检测结点的"左右子树是否都为空" void PreOrderPrintLeaves(BinTree BT) { if(BT) { if(!BT->Left && !BT->Right) printf("%d", BT->Data); PreOrderPrintLeaves(BT->Left); PreOrderPrintLeaves(BT->Right); } } // 求二叉树的高度 // Height = max(HL, HR) + 1 (通过改造后序遍历) 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; }