03-树2 List Leaves (25分)

来源:互联网 发布:淘宝刷真实流量平台 编辑:程序博客网 时间:2024/05/16 10:05

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

按照从上到下,从左到右的顺序列出给出的树的所有叶结点。

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer NN (\le 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N1. Then NN 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.

每个用例中,第一行给出表示树的全部结点总数N,N是一个小于10的正整型,结点编号为0至N-1。接下来的N行,每一行对应一个结点,给出左孩子和有右孩子的编号。如果不存在孩子,则在位置上放一个“-”。每一对孩子用空格间隔开。

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

按照从上到下,从左到右的顺序在同一行列出所有叶结点的编号。相邻的数字用空格间隔开,行末不能有多余的空格。

Sample Input:

81 -- -0 -2 7- -- -5 -4 6

Sample Output:

4 1 5

思路:

从上到下,从左到右的顺序列出叶结点,那么就是用层次遍历的方法找出叶结点即可。用队列来实现层次遍历。我不太会写C++,队列的头文件#include <queue>的代码是这样的:

#include <queue>  using namespace std; //这几个头文件必不可少  int main()  {  queue<int> q; //使用前需定义一个queue变量,且定义时已经初始化  while(!q.empty()) q.pop(); //重复使用时,用这个初始化  q.push(1); //进队列  q.pop(); //出队列  int v=q.front(); //得到队首的值  int s=q.size(); //得到队列里元素个数  return 0;  

输出时行末不能有多余的空格,如果是用“输出数字+输出空格”的方法,就要判断是否是最后一个数,比较麻烦。可以换一种思路,既“输出空格+输出数字”的方法。第一个数不输出空格,其他每个数字之前都输出空格,就可以解决输出问题。
有一半以上的代码和03-树1 树的同构(25分)是相同的。

#include <cstdio>#include <cstdlib>#include <iostream>#include <queue>#define MaxTree 10#define Null -1//NULL用于指针和变量上,值是0;因为要用静态链表,//NULL作为下标仍有对应元素,故用Null定义-1 #define Tree int#define ElementType char  using namespace std;queue<int> q; //一个int类型的队列q struct TreeNode{Tree Left;Tree Right;}T[MaxTree];Tree BulidTree(struct TreeNode T[])//输入数据的函数 {int N;scanf("%d\n",&N); int check[N];//用于判断是否为根节点的数组 int Root=Null;ElementType cl,cr;if(N>0){for(int i=0;i<N;i++){check[i]=0;}for(int i=0;i<N;i++){scanf("%c %c\n",&cl,&cr);if(cl != '-'){                T[i].Left = cl - '0';//字符减去字符0,得到int类型的该字符。'0'的ascii码为48,字符'3'为51,'3'-'0'=3                 check[T[i].Left] = 1;//若一个结点有子节点,则子节点不可能为根节点,故子节点对应的check元素赋值为1             }            else                T[i].Left = Null;            if(cr != '-'){                T[i].Right = cr - '0';                check[T[i].Right] = 1;            }            else                T[i].Right = Null;        }        for(int i=0;i<N;i++)        {            if(!check[i])//找出根节点 {                Root = i;                break;            }        }}return Root;}void countleaves(Tree Root)//把一个结点的左右孩子入队,然后依次从队头取出判断是否为叶子节点,否则重复将其左右孩子入队 {Tree temp;int count=0;//用于辅助计算结点 if(Root==Null){return;}q.push(Root);while(!q.empty()){temp=q.front();q.pop();if(T[temp].Left==Null&&T[temp].Right==Null)//没有左右孩子,就是叶结点,输出 {if(count++!=0){printf(" ");//不是第一个叶节点的话前面输出空格}printf("%d",temp);}if(T[temp].Left!=Null){q.push(T[temp].Left);}if(T[temp].Right!=Null){q.push(T[temp].Right);}}}int main(){Tree R;R=BulidTree(T);countleaves(R);return 0;} 


                                             
0 0
原创粉丝点击