~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:vs2017,C++
作者:
撰写时间:19年8月7日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream>
using namespace std;
struct Ertree
{
char name;
Ertree *left;
Ertree *right;
};
void SumErtree(Ertree *tree, int &n);
void CreatTree();
void main()
{
CreatTree();
}
void SumErtree(Ertree *tree, int &n)//遍历节点
{
if (tree==NULL)
{
return;
}
if(tree->left==NULL&&tree->right==NULL)
{
n++;
}
SumErtree(tree->left,n);
SumErtree(tree->right,n);
}
void CreatTree()//创建节点
{
Ertree treeA = { 'A',NULL,NULL };
Ertree treeB = { 'B',NULL,NULL };
Ertree treeC = { 'C',NULL,NULL };
Ertree treeD = { 'D',NULL,NULL };
Ertree treeE = { 'E',NULL,NULL };
Ertree treeF = { 'F',NULL,NULL };
Ertree treeG = { 'G',NULL,NULL };
Ertree treeH = { 'H',NULL,NULL };
Ertree treeJ = { 'J',NULL,NULL };
Ertree treeK = { 'K',NULL,NULL };
treeA.left = &treeF;
treeA.right = &treeK;
treeF.left = &treeD;
treeF.right = &treeE;
treeD.left = &treeC;
treeD.right = &treeB;
treeK.right = &treeJ;
treeJ.left = &treeH;
treeJ.right = &treeG;
int n = 0;
SumErtree(&treeA, n);
cout << "叶子结点数目:"<<n<<endl;
}
开发工具与关键技术:vs2017,C++
作者:
撰写时间:19年8月7日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream>
using namespace std;
struct Ertree
{
char name;
Ertree *left;
Ertree *right;
};
void SumErtree(Ertree *tree, int &n);
void CreatTree();
void main()
{
CreatTree();
}
void SumErtree(Ertree *tree, int &n)//遍历节点
{
if (tree==NULL)
{
return;
}
if(tree->left==NULL&&tree->right==NULL)
{
n++;
}
SumErtree(tree->left,n);
SumErtree(tree->right,n);
}
void CreatTree()//创建节点
{
Ertree treeA = { 'A',NULL,NULL };
Ertree treeB = { 'B',NULL,NULL };
Ertree treeC = { 'C',NULL,NULL };
Ertree treeD = { 'D',NULL,NULL };
Ertree treeE = { 'E',NULL,NULL };
Ertree treeF = { 'F',NULL,NULL };
Ertree treeG = { 'G',NULL,NULL };
Ertree treeH = { 'H',NULL,NULL };
Ertree treeJ = { 'J',NULL,NULL };
Ertree treeK = { 'K',NULL,NULL };
treeA.left = &treeF;
treeA.right = &treeK;
treeF.left = &treeD;
treeF.right = &treeE;
treeD.left = &treeC;
treeD.right = &treeB;
treeK.right = &treeJ;
treeJ.left = &treeH;
treeJ.right = &treeG;
int n = 0;
SumErtree(&treeA, n);
cout << "叶子结点数目:"<<n<<endl;
}