PAT 1110 Complete Binary Tree[比较]

1110 Complete Binary Tree (25 分)

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

题目大意:给出一个二叉树,判断是否是完全二叉树。

//这个题真的学到了不少东西。我的AC:

参照了:https://blog.csdn.net/hyf20144055065/article/details/51970789

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stdio.h>
#include <queue>
#include<cmath>
#include <vector>
using namespace std;
struct Node{
    int left=-1,right=-1;
}node[20];
int isr[20];
int root=-1;
int ct=0,n,last=0;
void level(int r){//进行层次遍历。我是选择在入队的时候+,但是这样是比较复杂的,我应该在出队的时候+。
    queue<int> qu;
    qu.push(r);
    //ct++;
    while(!qu.empty()){
        //if(ct==n)last=qu.back();
        int top=qu.front();qu.pop();
        if(top==-1)break;
        last=top;//每次last都赋值为当前。
        ct++;
//        if(node[top].left==-1)break;
//        else {
//            qu.push(node[top].left);
//            ct++;
//        }
        qu.push(node[top].left);//其实这里-1完全可以push进去,因为下一次再次弹出时会进行判断的。
        qu.push(node[top].right);
//        if(node[top].left!=-1){
//            qu.push(node[top].right);
//            ct++;
//        }
    }
}

int main()
{
    cin>>n;
    string l,r;
    for(int i=0;i<n;i++){
        cin>>l>>r;
        if(l!="-"){
            node[i].left=atoi(l.c_str());//注意这里的转换
            //cout<<node[i].left<<‘\n‘;
            isr[node[i].left]=1;
        }
        if(r!="-"){
            node[i].right=atoi(r.c_str());
            isr[node[i].right]=1;
        }
    }
    for(int i=0;i<n;i++){
        if(isr[i]==0){
            root=i;break;
        }
    }
    level(root);
    if(ct==n)
        cout<<"YES "<<last;
    else
        cout<<"NO "<<root;
    return 0;
}

1.利用完全二叉树的性质来判断。

2。使用了层次遍历,但是我的层次遍历思路是不正确的。

3.我的思路:只要有左子节点就push进去,然后计数+1,是在入队的时候计算数量。只要左子节点为-1,那么就break掉。

4.正确思路:当节点左右为空-1时,可以push进去,在whlle循环中会进行判断,如果是-1,那么就break了,是在出队的时候进行+1操作,这样最后就可以了

5.在我原来的那种方法中,2和6测试点过不去,对于6测试点:

使用了

1

- -

测试数据进行了更正

2测试点是对于0的检测。

正确输出应该是YES  0

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9939813.html

时间: 2024-07-30 10:17:43

PAT 1110 Complete Binary Tree[比较]的相关文章

PAT甲级——1110 Complete Binary Tree (完全二叉树)

此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary Tree (25 分) 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 eac

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

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 (≤) which is the total number of nodes in the tree -- and he

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

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

判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,root; const int maxn=30; struc

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace

PAT1110:Complete Binary Tree

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, th

PAT 1064 Complete Binary Search Tree

1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <vector> 5 #include <algorithm> 6 7 using namespace std; 8 9 class Node { 10 public: 11 int val; 12 Node* left; 13 Node* right; 14 public: 15 Node(): v

leetcode_919. Complete Binary Tree Inserter

https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root; CBTInserter.insert(int v) will insert a TreeNode into the t

[CF792D] Paths in a Complete Binary Tree (规律, 位运算, lowbit)

题目链接:http://codeforces.com/problemset/problem/792/D 画出树,找找规律,画图就好了.不算麻烦. 往下走的时候特判是不是叶子,往上走的时候特判是不是根.其余时候按照规律转移就是. 感觉可以推广到建树上,可以缩小常数是极好的. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 LL lowbit(LL x) { return x & (