03-树2 List Leaves(25 分)
来源:互联网 发布:黑马程序员培训 编辑:程序博客网 时间:2024/05/21 10:09
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 N (≤10) 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 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:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
题目要求
给出一个二叉树,你需要按从上到下,从左到右的顺序输出所有的叶结点。
输入要求
- 第一行输出树的总结点N,并且0<=N<=10
- 结点从0-N-1进行标记
- 有N行,每行对应一个结点,给出结点的左右儿子,之间用一个空格隔开
- 如果儿子不存在,则用“-”表示
输出要求
在一行中按从上到下,从左到右的顺序列出所有的叶结点,结点之间用一个空格隔开,在行的尾部没有多余的空格。
用c++编的代码如下:
#include <stdio.h>#include <stdlib.h>#include <queue>using namespace std;#define MaxTree 10#define ElementType char#define Tree int#define Null -1queue<int> q;struct TreeNode{ ElementType Element; Tree Left; Tree Right;}T1[MaxTree];int BuildTree(struct TreeNode T[]);void FineLeaves(Tree R);int main(){ Tree R; R = BuildTree(T1); FineLeaves(R); return 0;}\\创建二叉树 int BuildTree(struct TreeNode T[]){ int N,i; scanf("%d\n",&N); char cl,cr; Tree check[MaxTree],Root = Null; if(N) { for (i=0;i<N;i++) { check[i] = 0; } for(i=0;i<N;i++) { scanf("%c %c\n",&cl,&cr); if(cl != '-') { T[i].Left = cl-'0'; check[T[i].Left] = 1; } else T[i].Left = Null; if(cr != '-') { T[i].Right = cr-'0'; check[T[i].Right] = 1; } else T[i].Right = Null; } for(i=0;i<N;i++) { if(!check[i]) { Root = i; break; } } } return Root;}\\找出二叉树的叶结点 void FineLeaves(Tree R){ int flag = 0; if(R == Null) return; q.push(R); int temp = 0; while(!q.empty()) { temp = q.front(); if((T1[temp].Left == Null) &&(T1[temp].Right == Null)) { if(!flag) { flag = 1; } else printf(" "); printf("%d",temp); } if(T1[temp].Left != Null) q.push(T1[temp].Left); if(T1[temp].Right != Null) q.push(T1[temp].Right); q.pop(); }}
结论:本来打算是用c写的,但发现c中没有queue的库,没办法采用C++写,本人对C++并不是很熟悉,所以写的有点粗糙,有问题的地方,欢迎指正。
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves (25分)
- 03-树2 List Leaves(25 分)
- 03-树2 List Leaves(25 分)
- PTA 03-树2 List Leaves(25 分)
- 黑莓开始提供zIPS手机安全服务,自家系统居然不能用?!
- 关于微信应用号,这些事儿你也许会关心
- mac idea 速度突然变慢
- [LeetCode] 461. Hamming Distance
- HDOJ1576 扩展欧几里得
- 03-树2 List Leaves(25 分)
- Spring Cloud与微服务学习总结(4)——认证鉴权与API权限控制在微服务架构中的设计与实现(二)
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- matlab学习笔记1-clc,close,close all,clear,clear all作用区别
- ruby eql?
- 2017.10.28一试
- Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'包名冲突
- Java_字符串模式匹配
- c语言中typedef的几种用法