题目:
编写递归算法,计算二叉树中叶子结点的数目
//算法思想:后序遍历 先求左右子树的叶子结点,再加上根结点的情况
#include<iostream>
#include<stack>
using namespace std;
#define TElemType double
typedef struct BiTNode
{
TElemType data;
bool flag;
struct BiTNode* lchild,* rchild;
}BiTNode,* BiTree;
void Creat_BiTree(BiTree& T)//创建二叉树
{
TElemType num;
if(cin>>num){
T=new BiTNode;
if(!T) exit(-2);
T->data=num;
Creat_BiTree(T->lchild);
Creat_BiTree(T->rchild);
}
if(!cin){
T=nullptr;
cin.clear();
cin.sync();
}
}
void PostOrder_UsingStack(BiTree T)//后序非递归遍历
{
stack<BiTree> S;
BiTree p=T;
while(p||!S.empty()){
while(p){
p->flag=true;
S.push(p);
p=p->lchild;
}
while(!S.empty()&&!S.top()->flag){
cout<<S.top()->data<<endl;