输出用先序遍历创建的二叉树是否为完全二叉树的判定结果

题目:输出用先序遍历创建的二叉树是否为完全二叉树的判定结果

这道题的后台测试数据特别水,导致很多错误的代码也可以ac,这里是博主在看了许多其他大佬的代码之后找到的较为简单的正确答案

 

    利用结构体指针队列进行层次遍历,只要节点不为空,就将左右孩子全部入队列(NULL也进),因为是完全二叉树,所以在第一次遇到NULL之前的结点数应该等于总节点数

代码如下:

/*
 * ━━━━━━神兽出没━━━━━━
 *    ┏┓   ┏┓
 *   ┏┛┻━━━┛┻┓
 *   ┃       ┃
 *   ┃   ━   ┃
 *   ┃ ┳┛ ┗┳ ┃
 *   ┃       ┃
 *   ┃   ┻   ┃
 *   ┃       ┃
 *   ┗━┓   ┏━┛      Code is far away from bug with the animal's protecting
 *     ┃   ┃          神兽保佑,代码无bug
 *     ┃   ┃
 *     ┃   ┗━━━┓
 *     ┃       ┣━┓
 *     ┃       ┏━┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 *
 * ━━━━━━大佬带带我━━━━━━
 */
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#define MAX 0xfffffff
#define MIN -0xfffffff
#define inf 0x3f3f3f3f
#define LL long long int
#define INIT(a) memset(a,0,sizeof(a))
const double Pi = acos(-1);
const double E = exp(1.0); 
const LL mod =1e9+7;
using namespace std;
typedef struct node
{
	char data;
	struct node *left;
	struct node *right;
}Node;
int sum=0;
void creat(Node *&p)
{
	char x;
	cin>>x;
	if(x=='#')return;
	sum++;
	p=new Node;
	p->data =x;
	p->left =NULL;
	p->right =NULL;
	creat(p->left );
	creat(p->right );
}
int main()
{
	Node *tree;
	creat(tree);
	Node *dui[1000];
	int tou=0,wei=0;
	dui[wei]=tree;wei++;
	while(tou!=wei)
	{
		if(dui[tou]==NULL)
		{
			if(tou==sum)cout<<"Y";
			else cout<<"N";
			break;
		}
		dui[wei]=dui[tou]->left ;wei++;
		dui[wei]=dui[tou]->right ;wei++;
		tou++;
	}
	
	return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值