其它pta数据结构编程题请参见:pta

这次的编程作业要求从上到下,从左到右输出一棵树的叶子节点,即树的层序遍历,用队列的方式来实现。

注意enqueue和dequeue函数参数为Queue &q,而不是Queue q,即用引用的方式才能改变实参的值。

 #include <iostream>
using namespace std; struct Node
{
int left;
int right;
}; struct Queue
{
int data[];
int first = -;
int end = -;
int maxSize = ;
}; int buildTree(Node tree[]);
void enqueue(Queue &q, int i);
int dequeue(Queue &q);
bool empty(Queue q); int main()
{
Node tree1[];
int root = buildTree(tree1);
Queue q, leaves;
int t, cnt = ;
enqueue(q, root);
while (!empty(q))
{
t = dequeue(q);
if (tree1[t].left == - && tree1[t].right == -)
enqueue(leaves, t);
if (tree1[t].left != -) enqueue(q, tree1[t].left);
if (tree1[t].right != -) enqueue(q, tree1[t].right);
}
while (!empty(leaves))
{
if (cnt > ) cout << " ";
cout << dequeue(leaves);
cnt++;
}
return ;
} int buildTree(Node tree[])
{
int num, i, root = -;
char l, r;
cin >> num;
int judgeRoot[] = {};
for (i = ; i < num; i++)
{
cin >> l >> r;
if (l != '-')
{
tree[i].left = l - '';
judgeRoot[l - ''] = ;
}
else tree[i].left = -;
if (r != '-')
{
tree[i].right = r - '';
judgeRoot[r - ''] = ;
}
else tree[i].right = -;
}
for (i = ; i < num; i++)
{
if (judgeRoot[i] == ) root = i;
}
return root;
} void enqueue(Queue &q, int i)
{
q.end = (q.end + ) % ;
q.data[q.end] = i;
} int dequeue(Queue &q)
{
q.first = (q.first + ) % ;
return q.data[q.first];
} bool empty(Queue q)
{
return q.end == q.first;
}

pta 编程题7 List Leaves的更多相关文章

  1. pta 编程题20 旅游规划

    其它pta数据结构编程题请参见:pta 题目 这个最短路径问题只需要求两点之间的最短路径,因而在Dijikstra算法中当求出目标点的最短路径之后跳出循环即可. #include <iostre ...

  2. pta 编程题21 公路村村通

    其它pta数据结构编程题请参见:pta 题目 这道题考察最小生成树问题,用的是Prim算法. 和Dijkstra算法相比,没有了collect数组,因为dist[v] == 0就代表v被已收录. #i ...

  3. pta编程题19 Saving James Bond 2

    其它pta数据结构编程题请参见:pta 题目 和简单版本不同的是,简单版本只需判断能否到达岸边,而这个版本要求求出最少跳数的路径. 简单版本用dfs实现,而这道题用BFS实现. 注意: 岛半径为7.5 ...

  4. pta 编程题16 Saving James Bond - Easy Version

    其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...

  5. pta 编程题15 列出连通集

    其它pta数据结构编程题请参见:pta 题目 题目要求分别以深度优先搜索和广度优先搜索输出图的连通集. 广度优先搜索要用到队列,先回顾一下循环队列: struct QNode { int* Data; ...

  6. pta 编程题14 Huffman Codes

    其它pta数据结构编程题请参见:pta 题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路 ...

  7. pta 编程题13 File Transfer

    其它pta数据结构编程题请参见:pta 这道题考察的是union-find并查集. 开始把数组中每个元素初始化为-1,代表没有父节点.为了使树更加平衡,可以让每一个连通分量的树根的负值代表这个连通分量 ...

  8. pta 编程题12 堆中的路径

    其它pta数据结构编程题请参见:pta 这道题考察的是最小堆. 堆是一个完全二叉树,因此可用数组表示,一个下标为 i 的结点的父节点下标为 i / 2,子结点下标为 2i 和 2i + 1. 插入元素 ...

  9. pta 编程题10 Root of AVL Tree

    其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...

随机推荐

  1. 将一个mapList转换为beanList

    public static <T> List<T> copyMapToBean(   List<Map<String, Object>> resultM ...

  2. MVC过滤大法(过滤静态文件)

    参考文章:https://prerakkaushik.wordpress.com/2014/02/12/routing-request-for-static-files-with-or-without ...

  3. LeetCode: 455 Assign Cookies(easy)

    题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...

  4. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)D(思维,DP,字符串)

    #include<bits/stdc++.h>using namespace std;char c[2007][2007];char ans[4007];int s[2007][2007] ...

  5. Oracle中 row_number() over()分析函数(转)

    https://www.cnblogs.com/moon-jiajun/p/3530035.html

  6. 洛谷P2828 Switching on the Lights(开关灯)

    P2828 Switching on the Lights(开关灯) 题目背景 来源:usaco-2015-dec Farm John 最近新建了一批巨大的牛棚.这些牛棚构成了一个N*N的矩形网络.( ...

  7. 洛谷P1962 斐波那契数列

    传送门 不难得到状态转移矩阵 然后带进去乱搞 //minamoto #include<iostream> #include<cstdio> #include<cstrin ...

  8. JSP读取Oracle数据库里的图片Blob字段并显示在页面上

    1.java代码: /** * 打印模板获取电子签名 * @param request * @param resp * @param id * @return * @throws Exception ...

  9. springIOC源码解析之Bean的创建

    上一篇讲到了beanFactory的配置文件的解析和beanFactory的创建,都集中到了obtainFreshBeanFactory();这一句代码里了,本篇主要讲bean的创建过程 public ...

  10. 我的省选 Day -5

    Day -5 时间载着我们,一天又一天,呼啸而过. 已经记不清今天是Day 负几了,总之还有不到一个星期就要去参加选拔赛了. 写一下今晚做NOI2009的心路历程. T1题意有点绕,但很快看出是个二分 ...