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, 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和遍历次数cnt是否相等,相等yes,不想等no。注意:输入用string而不用char,因为char不能表示两位数以上的数字。之前没注意导致代码只有部分ac。

代码

 

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

class Node
{
public:
    int left;
    int right;
};
int main()
{
    int N;
    while(cin >> N)
    {
      vector<Node> nodes(N);
      vector<bool> isroot(N,true);
      //Build tree
      for(int i = 0;i < N;i++)
      {
          string left,right;
          cin >> left >> right;
          if(left == "-")
            nodes[i].left = -1;
          else
          {
            nodes[i].left = stoi(left);
            isroot[nodes[i].left] = false;
          }
          if(right == "-")
            nodes[i].right = -1;
          else
          {
            nodes[i].right = stoi(right);
            isroot[nodes[i].right] = false;
          }
      }
      //Find root
      int root = -1;
      for(int i = 0;i < isroot.size();i++)
      {
          if(isroot[i])
          {
            root = i;
            break;
          }
      }
      //BFS
      queue<int> q;
      q.push(root);
      int cnt = 0,lastindex = -1;
      while(!q.empty())
      {
          int tmp = q.front();
          q.pop();
          if(tmp == -1)
          {
              break;
          }
          cnt++;
          lastindex = tmp;
          q.push(nodes[tmp].left);
          q.push(nodes[tmp].right);
      }
      //Output
      if(cnt == N)
        cout << "YES" << " " << lastindex <<endl;
      else
        cout << "NO" << " " << root << endl;
    }
}

  

时间: 2024-11-16 04:44:39

PAT1110:Complete Binary Tree的相关文章

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 nu

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

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

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

[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 & (

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

Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)

1 vector<TreeNode> ve(16385,0); 2 class CBTInserter 3 { 4 public: 5 queue<TreeNode*> q; 6 int veEnd; 7 8 CBTInserter(TreeNode* root) 9 { 10 TreeNode rubbish(0); 11 veEnd = 0; 12 ve[veEnd++] = (rubbish); 13 q.push(root); 14 Init(); 15 for(int i

【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