1110 Complete Binary Tree (25 point(s))

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

题目大意有n个结点,这n个结点为0~n-1,给出这n个结点的左右孩子,问这棵树是不是完全二叉树。

分析:

1)树的读入:由于给出的是结点索引,因此采用静态链表存储二叉树。在存储过程中用vis【】数组标记一下索引结点,在这之后,没有被标记的就是根节点索引。

2)下面用两种方法判断是不是完全二叉树。

1:基于广度遍历:
层次遍历时将所有结点入队(包括非空结点),开一个cnt,记录层次遍历已经访问的结点总数。遍历过程中如果空结点后面还有非空结点,那就不是完全二叉树  -->转化为 遇到结点索引为-1时 cnt<n ,此时 flag标记一下 ( n为总结点数)。最后BFS()返回flag即可

2:基于深度遍历:

递归出最大的下标值,完全二叉树一定把前面的下标充满: 最大的下标值 == 最大的节点数;非完全二叉树前面一定有位置是空: 最大的下标值 > 最大的节点数~

完整代码:
one:基于广度遍历:

#include<bits/stdc++.h>
using namespace std;
struct node{
	int l,r;
}node[100];
bool vis[100];
int n,maxn = -1, ans;
bool BFS(int root,int &index){
	if(root==-1) return true;  
	queue<int>q;
	q.push(root);
	bool flag=true;
	int cnt=0; 
	while(!q.empty()){
		int now=q.front();
		q.pop();
		if(now!=-1){
			index=now;
			q.push(node[now].l );
			q.push(node[now].r );
		}else
			if(cnt<n) flag=false;
	}
	return flag;
}
		
int main(){
	int index=0,root=0;//index保存完全二叉树的最后一个结点索引
	cin>>n;
	for(int i=0;i<n;i++){
	    string l,r;
		cin>>l>>r;
		if(l=="-") node[i].l=-1;
		else {
			node[i].l=stoi(l);//stoi(l)是结点索引,也是被索引到的结点 
			vis[stoi(l)]=true;
		}
		if(r=="-") node[i].r =-1;
		else{
			node[i].r=stoi(r);
			vis[stoi(r)]=true;
		} 	
	}
	while(vis[root]) root++;
        if(BFS(root,index)) cout<<"YES "<<index;
	else cout<<"NO "<<root;
	return 0;
}

two:基于深度遍历

#include<bits/stdc++.h>
using namespace std;
struct node{
	int l,r;
}node[100];
bool vis[100];
int n,maxn = -1, ans;

void dfs(int root, int index) {
    if(index > maxn) { //index是索引的结点个数 
        maxn = index;
        ans = root; //保存根节点索引 
    }
    if(node[root].l != -1) dfs(node[root].l, index * 2);
    if(node[root].r != -1) dfs(node[root].r, index * 2 + 1);
}
			
int main(){
	int index=0,root=0;//index保存完全二叉树的最后一个结点索引
	cin>>n;
	for(int i=0;i<n;i++){
	    string l,r;
		cin>>l>>r;
		if(l=="-") node[i].l=-1;
		else {
			node[i].l=stoi(l); //stoi(l)是被索引到的结点 
			vis[stoi(l)]=true;
		}
		if(r=="-") node[i].r =-1;
		else{
			node[i].r=stoi(r);
			vis[stoi(r)]=true;
		} 	
	}
	while(vis[root]) root++;
    dfs(root, 1);
    if (maxn == n)
        cout << "YES " << ans;
    else
        cout << "NO " << root;
	return 0;
}

深搜代码好像更短一点哦。

That’s all !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值