【PAT】 A1004 Counting Leaves 错误经验 您所在的位置:网站首页 错误代码leaf 【PAT】 A1004 Counting Leaves 错误经验

【PAT】 A1004 Counting Leaves 错误经验

2023-07-29 07:48| 来源: 网络整理| 查看: 265

在这里插入图片描述

先上错误答案

在这里插入图片描述

错误原因:

输入的时候可能是先输入子节点的节点情况再输入父节点的节点情况。 如果给出: 3 2 01 1 02 02 1 03 可以正确输出。 但是如果换成这样给出: 3 2 02 1 03 01 1 02 输入的树和上面的一样,但是此时是2先输入,答案就会出错。

错误代码: #include #include #include using namespace std; const int maxn = 100; struct NODE { int level;//标记当前节点在第几层 vector child; }; bool visited[maxn] = { false }; int N, M; NODE n[maxn]; queue q; int leaves[maxn] = { 0 }; int hie = 0; void LayerOrder() { vector::iterator it; for (it = n[0].child.begin(); it != n[0].child.end(); ++it) { q.push(*it); } int tmp; while (!q.empty()) { tmp = q.front(); q.pop(); if (n[tmp].child.size() == 0) { ++leaves[n[tmp].level]; } for (it = n[tmp].child.begin(); it!= n[tmp].child.end(); ++it) { q.push(*it); } } } int main() { cin >> N >> M; if (N == 0) { cout //输入的时候,想当然的以为父节点会先输入,将没有访问过的节点都当作父节点处理,并赋值level为1. n[tmproot].level = 1; n[0].child.push_back(tmproot); } cin >> numofchild; for (int j = 0; j cout queue q; q.push(root); int last = root;//用last做层次上的标记,每次都标记每一层的最后一个节点 int tmp; int leaf=0; while (!q.empty()) { tmp = q.front(); if (Tree[tmp].size() == 0) ++leaf; else for (int i = 0; i cin >> N >> M; if (N == 0) { cout cin >> tmpchild; Tree[tmproot].push_back(tmpchild); } } LayerOrder(1); for (int i=0 ; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有