【PAT】【Advanced Level】1110. Complete Binary Tree (25)

14 篇文章 0 订阅
8 篇文章 0 订阅

1110. Complete Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a tree, you are supposed to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=20) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each case, print in one line "YES" and the index of the last node if the tree is a complete binary tree, or "NO" and the index of the root if not. There must be exactly one space separating the word and the number.

Sample Input 1:
9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -
Sample Output 1:
YES 8
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1

原题链接:

https://www.patest.cn/contests/pat-a-practise/1110

思路:

树的遍历

满二叉树的特征

CODE:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#define N 22

using namespace std;

typedef struct S
{
	int ind;
	int ls;
	int rs;
	int s;
	int fa;
	int tra;
	int fl;
};

S t[N];

int tr;

vector<int> ro;
vector<int> le;

vector<S> trav;
void dfs1(int n,int flo)
{
	t[n].fl=flo;
	if (t[n].ls!=-1)
	{
		dfs1(t[n].ls,flo+1);
	}
	t[n].tra=tr;
	tr++;
	trav.push_back(t[n]);
	if (t[n].rs!=-1)
	{
		dfs1(t[n].rs,flo+1);
	}
	return ;
}
bool cmp(S a,S b)
{
	if(a.fl==b.fl)
	{
		return a.tra<b.tra;
	}
	else
	{
		return a.fl<b.fl;
	}
}
int main()
{
	int n;
	cin>>n;
	for (int i=0;i<n;i++) t[i].fa=-1;
	for (int i=0;i<n;i++)
	{
		t[i].ind=i;
		t[i].s=0;
		string a,b;
		cin>>a>>b;
		if (a!="-")
		{
			t[i].ls=atoi(a.c_str());
			t[t[i].ls].fa=i;
			t[i].s++;
		}
		else
		{
			t[i].ls=-1;
		}
		if (b!="-")
		{
			t[i].rs=atoi(b.c_str());
			t[t[i].rs].fa=i;
			t[i].s++;
		}
		else
		{
			t[i].rs=-1;
		}
		//cout<<t[i].s<<endl;
		if (t[i].s!=0)
		{
			ro.push_back(i);
		}
		else
		{
			le.push_back(i);
		}
	}
	
	int root;
	for (int i=0;i<n;i++)
	{
		if (t[i].fa==-1)
		{
			root=i;
			break;
		}
	}
	
	tr=0;
	dfs1(root,0);
	sort(trav.begin(),trav.end(),cmp);
	//cout<<trav[n-1].ind<<endl;
	int flag=0;
	for (int i=0;i<ro.size();i++)
	{
		if (t[ro[i]].s!=2)
		{
			if (!(t[ro[i]].ls==trav[n-1].ind))
			{
				//cout<<ro[i]<<" "<<t[ro[i]].ls<<" "<<t[ro[i]].rs<<endl;
				flag=1;
				break;
			}
		}
	}
	if (flag==0)
	{
		for (int i=0;i<le.size();i++)
		{
			if (t[le[i]].fl<(trav[n-1].fl-1))
			{
				flag=1;
				break;
			}
			else if (t[le[i]].fl==(trav[n-1].fl-1))
			{
				if (t[le[i]].tra<trav[n-1].tra)
				{
					flag=1;
					break;
				}
			}
		}
	}
	if (flag==0)
	{
		cout<<"YES "<<trav[n-1].ind<<endl;
	}
	else
	{
		cout<<"NO "<<root<<endl;
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言实现的代码: ```c #include <stdio.h> #include <stdbool.h> #include <stdlib.h> /* 二叉树结点 */ struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; }; /* 队列结点 */ struct QueueNode { struct TreeNode* data; struct QueueNode* next; }; /* 队列 */ struct Queue { struct QueueNode* front; struct QueueNode* rear; }; /* 创建队列 */ struct Queue* createQueue() { struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue)); queue->front = queue->rear = NULL; return queue; } /* 判断队列是否为空 */ bool isQueueEmpty(struct Queue* queue) { return queue->front == NULL; } /* 入队 */ void enqueue(struct Queue* queue, struct TreeNode* data) { struct QueueNode* newNode = (struct QueueNode*)malloc(sizeof(struct QueueNode)); newNode->data = data; newNode->next = NULL; if (isQueueEmpty(queue)) { queue->front = queue->rear = newNode; } else { queue->rear->next = newNode; queue->rear = newNode; } } /* 出队 */ struct TreeNode* dequeue(struct Queue* queue) { if (isQueueEmpty(queue)) { return NULL; } else { struct TreeNode* data = queue->front->data; struct QueueNode* temp = queue->front; queue->front = queue->front->next; if (queue->front == NULL) { queue->rear = NULL; } free(temp); return data; } } /* 判断是否为完全二叉树 */ bool isCompleteTree(struct TreeNode* root) { if (root == NULL) { return true; } struct Queue* queue = createQueue(); enqueue(queue, root); bool flag = false; while (!isQueueEmpty(queue)) { struct TreeNode* temp = dequeue(queue); if (temp->left) { if (flag) { return false; } enqueue(queue, temp->left); } else { flag = true; } if (temp->right) { if (flag) { return false; } enqueue(queue, temp->right); } else { flag = true; } } return true; } /* 创建二叉树 */ struct TreeNode* createTree() { int val; scanf("%d", &val); if (val == -1) { return NULL; } struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); return root; } /* 主函数 */ int main() { struct TreeNode* root = createTree(); if (isCompleteTree(root)) { printf("It is a complete binary tree.\n"); } else { printf("It is not a complete binary tree.\n"); } return 0; } ``` 原理: 完全二叉树(Complete Binary Tree)是指除了最后一层外,其他层的结点数都达到了最大值,最后一层的结点都集中在左侧。而对于一棵二叉树,如果它的层数为 h,那么它最多有 $2^h-1$ 个结点。因此,我们可以利用层序遍历的方式,对二叉树进行遍历,如果遇到某个结点缺少左子结点或右子结点,那么后续的结点必须全部为叶子结点,否则这棵二叉树就不是完全二叉树。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值