Fetch the repository succeeded.
#include <iostream>
#include <queue>
using namespace std;
typedef struct TNode
{
	int data;
	TNode *left;
	TNode *right;
}*BinTree;
void Insert(BinTree &T, int a);
int BFS(BinTree T);
int main()
{
	int num;
	cin >> num;
	BinTree T;
	T = NULL;
	int a;
	for (int i = 0; i < num; i++)
	{
		cin >> a;
		Insert(T, a);
	}
	int printfnum;
	printfnum = BFS(T);//记录数量
	if (printfnum == num)
	{
		cout << endl << "YES";
	}
	else
	{
		cout << endl << "NO";
	}
}
void Insert(BinTree &T, int a)//建树
{
	if (T == NULL)
	{
		T = new TNode;
		T->data = a;
		T->left = NULL;
		T->right = NULL;
	}
	else
	{
		if (a < T->data)//一开始没注意到是大的在左,小的在右
		{
			return Insert(T->right, a);
		}
		else if (a > T->data)
		{
			return Insert(T->left, a);
		}
	}
}
int BFS(BinTree T)
{
	int count=0;
	int flag=0;//控制计数
	int sign = 0;//控制空格
	queue<BinTree>q;
	if (T == NULL)
	{
		cout << "NULL";
	}
	q.push(T);
	while (!q.empty())//进行层次遍历
	{
		if (q.front() == NULL)//遇到空结点停止计数
		{
			flag = 1;
		}
		else
		{
			if (sign == 0)
			{
				cout << q.front()->data;
				sign = 1;
			}
			else if (sign == 1)
			{
				cout << " " << q.front()->data;
			}
			if (flag == 0)
			{
				count++;
			}
			q.push(q.front()->left);
			q.push(q.front()->right);
		}
		q.pop();
	}
	return count;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。