文档介绍:二叉树叶子结点个数计算
D
}
}
void BiTree::yezi(BiNode *root,int &n)
{
if(root)
{
if(root->lchild==NULL&&root->rchild==NULL)
n++;
yezi(root->lchild,n);
yezi(root->rchild,n);
}
}
BiNode* BiTree::Creat( )
{
BiNode *root;
char ch;
cin>>ch;
if (ch=='#') root = NULL;
else{
root = new BiNode; //生成一个结点
root->data=ch;
root->lchild = Creat( ); //递归建立左子树
root->rchild = Creat( ); //递归建立右子树
}
return root;
}
void BiTree::Release(BiNode *root)
{
if (root!= NULL){
Release(root->lchild); //释放左子树
Release(root->rchild); //释放右子树
delete root;
}
}
void main()
{ cout<<"请输入二叉树的结点数据:";
BiTree bt; //创建一棵树
BiNode *root = ( ); //获取指向根结点的指针
int n=0;
cout<<"------前序遍历------ "<<endl;
(root);
(root,n);
cout<<endl;
cout<<"叶子节点数:"<<n;
cout<<endl;
}
非递归算法求叶子结点的个数
#include<stack>
#include<iostream>
using namespace std;
struct node
{
int data;
node *lchild;
node *rchild;
};
node *root=NULL;
void mid(node*root,int key=500)
{
int sum=0;
stack<node*>s;
while(NULL!=root || !())
{
if(NULL!=root)
{
(root);
root=root->lchild;
}
else
{
root=();
// cout<<root->data<<" ";
if(NULL==root->lchild &