首页 > 其他分享 >1110 Complete Binary Tree(附测试点2,3,4,6分析)

1110 Complete Binary Tree(附测试点2,3,4,6分析)

时间:2023-05-24 10:33:29浏览次数:49  
标签:tmp Binary 结点 Complete 测试点 int nodes include root

题目:

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个结点,这n个结点为0~n-1,给出这n个结点的左右孩子,求问这棵树是不是完全二叉树

 

思路:

测试点2,3,4: 

因为结点可能是两位数,我之前用char类型去获取左右孩子结点,但是结点可能是两位数,所以需要用string

测试点6:

当结点个数为1时,一定是完全二叉树

 

如何判断是完全二叉树?

除了最底层之外,每一层的结点个数为2^n

最底层的结点一定是从左至右“填充”

 

易错点:

scanf("%c")输入字符时,需要注意: %c前没空格,scanf()将读取标准输入流中的第一个字符,%c前有空格,scanf()则读取标准输入流中第一个非空白字符,屏蔽了空白字符

 

代码:(满分)

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<math.h>
using namespace std;
int n, root, treeDeep = -1;
bool isRoot[25];
vector<int> deep[25];
struct Node{
    int d, lchild, rchild;
}nodes[25];
void bfs(int root){
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int tmp = q.front();
        q.pop();
        deep[nodes[tmp].d].push_back(tmp);
        if(nodes[tmp].d > treeDeep) treeDeep = nodes[tmp].d;
        int l = nodes[tmp].lchild;
        int r = nodes[tmp].rchild;
        if(l != -1){
            nodes[l].d = nodes[tmp].d + 1;
            q.push(l);
        }
        if(r != -1){
            nodes[r].d = nodes[tmp].d + 1;
            q.push(r);
        }
    }
}
int main(){
    scanf("%d", &n);
    fill(isRoot, isRoot + n, 1);
    for(int i = 0; i < n; i++){
        string l, r;
        cin>>l>>r;
        // cout<<l<<" "<<r<<endl;
        if(l != "-"){
            nodes[i].lchild = atoi(l.c_str());
            isRoot[atoi(l.c_str())] = 0;
        }else{
            nodes[i].lchild = -1;
        }
        if(r != "-"){
            nodes[i].rchild = atoi(r.c_str());
            isRoot[atoi(r.c_str())] = 0;
        }else{
            nodes[i].rchild = -1;
        }
    }
    
    for(int i = 0; i < n; i++){
        if(isRoot[i]){
            root = i;
            break;
        }
    }
    nodes[root].d = 0;
    bfs(root);
    bool flag = true;
    for(int i = 0; i < treeDeep; i++){
        if(pow(2, i) != deep[i].size()){
            flag = false;
        }
    }
    int num = 0, lastNode = 0;
    if(treeDeep == 0){
        printf("YES %d", lastNode);
        return 0;
    }
    for(int i = 0; i < deep[treeDeep - 1].size(); i++){
        int node = deep[treeDeep - 1][i];
        if(nodes[node].lchild == -1 && nodes[node].rchild != -1){
            flag = false;
            break;
        }
        if(nodes[node].lchild == -1 && nodes[node].rchild == -1){
            if((pow(2, treeDeep) - 1) + num != n){
                flag = false;
            }
            break;
        }
        if(nodes[node].lchild != -1 && nodes[node].rchild == -1){
            num++;
            if((pow(2, treeDeep) - 1) + num != n){
                flag = false;
            }
            lastNode = nodes[node].lchild;
            break;
        }
        if(nodes[node].lchild != -1){
            num++;
        }
        if(nodes[node].rchild != -1){
            num++;
            lastNode = nodes[node].rchild;
        }
        
    }
    if(!flag){
        printf("NO %d", root);
    }else{
        printf("YES %d", lastNode);
    }
    return 0;
}

 

优化思路:递归出最大的下标值,完全二叉树一定把前面的下标充满: 最大的下标值 == 最大的节点数;不完全二叉树前满一定有位置是空,会往后挤: 最大的下标值 > 最大的节点数

 

优化代码:(更简单)

 

#include <iostream>
using namespace std;
struct node{
    int l, r;
}a[100];
int maxn = -1, ans;
void dfs(int root, int index) {
    if(index > maxn) {
        maxn = index;
        ans = root;
    }
    if(a[root].l != -1) dfs(a[root].l, index * 2);
    if(a[root].r != -1) dfs(a[root].r, index * 2 + 1);
}
int main() {
    int n, root = 0, have[100] = {0};
    cin >> n;
    for (int i = 0; i < n; i++) {
        string l, r;
        cin >> l >> r;
        if (l == "-") {
            a[i].l = -1;
        } else {
            a[i].l = stoi(l);
            have[stoi(l)] = 1;
        }
        if (r == "-") {
            a[i].r = -1;
        } else {
            a[i].r = stoi(r);
            have[stoi(r)] = 1;
        }
    }
    while (have[root] != 0) root++;
    dfs(root, 1);
    if (maxn == n)
        cout << "YES " << ans;
    else
        cout << "NO " << root;
    return 0;
}

 

标签:tmp,Binary,结点,Complete,测试点,int,nodes,include,root
From: https://www.cnblogs.com/yccy/p/17427282.html

相关文章

  • 1099 Build A Binary Search Tree
    题目:ABinarySearchTree(BST)isrecursivelydefinedasabinarytreewhichhasthefollowingproperties:Theleftsubtreeofanodecontainsonlynodeswithkeyslessthanthenode'skey.Therightsubtreeofanodecontainsonlynodeswithkeysg......
  • AtCoder Regular Contest 132 D Between Two Binary Strings
    洛谷传送门AtCoder传送门提供一个dp思路。下文设串长为\(n\),串中\(1\)个数为\(m\)。考虑如何求\(d(s,t)\)。设\(s\)的\(1\)位置分别为\(a_1,a_2,...,a_m\),\(t\)的\(1\)位置分别为\(b_1,b_2,...,b_m\)。那么\(d(s,t)=\sum\limits_{i=1}^m|a_i-b......
  • 如何在gdb中打印<incomplete type>变量 gdb
    linkvimain1.cpp#include<iostream>#include<fstream>#include<stdio.h>usingnamespacestd;intmain(){ifstreaminFile("test.txt",ios::in);//printf("%p",inFile);cout<<"inFile="&l......
  • 如何列举测试点
    测试人员需要能够在软件开发过程中,基于软件的需求文档或者功能说明书,准确的识别和描述每一个功能点。列举功能点是测试人员的必备技能之一,因为测试人员需要从功能的角度来评估软件的质量,以确保软件的功能符合用户的期望和需求。通过列举功能点,测试人员可以更好地了解软件的功能,从......
  • Waves 14 Complete Mac (Waves混音效果全套插件)
    Waves14Complete是一款全功能的音频处理软件套装,包含超过140个插件,可用于各种音频处理和音乐制作任务。这个套装包含了多种不同类型的插件,包括均衡器、压缩器、混响、延迟、合成器、调制器等等。Waves14Complete还提供了许多专业级功能,如自适应限制、自动启动时间校准、360度......
  • 1102 Invert a Binary Tree
    题目:ThefollowingisfromMaxHowell@twitter:Google:90%ofourengineersusethesoftwareyouwrote(Homebrew),butyoucan'tinvertabinarytreeonawhiteboardsofuckoff. Nowit'syourturntoprovethatYOUCANinvertabinarytree!I......
  • CF1228D Complete Tripartite
    有些题解够了,这题和三分图的判定没有什么关系……这里主要是一个转化,一个点会和所以不与自己相连的点处于相同的集合中。换句话说,如果两个点在同一个集合内,那与这两个点相连的点的集合是完全相同的。这里使用了哈希来判定,另外,如果有孤立的点存在,则要特判。constintmaxN=1e5+......
  • C#中的Task.CompletedTask和Task.Result学习
    在学习C#中的Task方法时,可以知道Task启动一个异步线程方法可以用Task.Run()进行,具体可以参看博客 https://www.cnblogs.com/yaosj/p/10342883.html和 https://www.cnblogs.com/wynblogscc/p/15138423.html但是,在有些返回类型是Task的方法中,可以在不进行异步的情况下计算结果.......
  • bits of static binary analysis阅读随笔
    标题是bitsofstaticbinaryanalysis,意思为碎片化粒度的静态二进制分析作为安全研究人员,如何从复杂系统的二进制文件中挖掘出漏洞是个难题在这篇随笔中,阅读bitsofstaticbinaryanalysis演讲内容,学习查找二进制程序中的漏洞,并快速找到它们静态分析,使用VEX和CFG来表示分析......
  • ctags和youcompleteme的比较
    ctags和youcompleteme是vim常用的两个代码提示工具。前者更古老简便,后者更先进。他们都是很优秀的软件工具,这里对他们进行对比梳理,以达到灵活使用他们的目的。基本使用介绍。ctags是vim内在就支持的,ctags-R产生tags文件,vim中通过settags=/path/to/tags文件,即可达到使用tags文......