程序:

 1 #include <stdio.h>
2 #include <queue>
3 #define MaxTree 20
4 #define Null -1
5 using namespace std;
6
7 struct TreeNode {
8 int Left;
9 int Right;
10 } T[MaxTree];
11 int N,check[MaxTree];
12 int count = 0;
13
14 int BuildTree(struct TreeNode T[]) {
15 int Root = Null,i;
16 char cl,cr;
17 scanf("%d\n",&N);
18 if(N) {
19 for(i=0; i<N; i++) check[i]=0;
20 for(i=0; i<N; i++) {
21 scanf("%c %c\n",&cl,&cr);
22 if(cl=='-' && cr=='-') count++;
23 if(cl!='-') {
24 T[i].Left = cl-'0';
25 check[T[i].Left]=1;
26 } else T[i].Left=Null;
27 if(cr!='-') {
28 T[i].Right = cr-'0';
29 check[T[i].Right]=1;
30 } else T[i].Right=Null;
31 }
32 for(i=0; i<N; i++)
33 if(!check[i]) break;
34 Root = i;
35 }
36 return Root;
37 }
38
39 int main() {
40 queue<int> Q;
41 int R,tmp;
42 R=BuildTree(T);
43 if(R==Null) return 0;
44 Q.push(R);
45 while(!Q.empty()) {
46 tmp = Q.front();
47 Q.pop();
48 if(T[tmp].Left==-1 && T[tmp].Right==-1){
49 printf("%d",tmp);
50 count--;
51 if(count!=0) printf(" ");
52 }
53 if(T[tmp].Left!=-1) Q.push(T[tmp].Left);
54 if(T[tmp].Right!=-1) Q.push(T[tmp].Right);
55 }
56 return 0;
57 }

分析:

1、利用队列做层序遍历

2、17行scanf()一开始没写\n,如果只有这一行输入没事,但后面还有scanf(),而%c又是可以识别\n的,所以会导致错误,详见:

scanf()的陷阱

https://blog.csdn.net/ff_tt/article/details/61429268

[刷题] PTA 03-树2 List Leaves的更多相关文章

  1. [刷题] PTA 03-树1 树的同构

    程序: 1 #include <stdio.h> 2 #define MaxTree 10 3 #define ElementType char 4 #define Tree int 5 ...

  2. LeetCode 刷题笔记 (树)

    1.  minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...

  3. [刷题] PTA 03-树3 Tree Traversals Again

    用栈实现树遍历 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXSIZE 30 4 5 int Pre[MAXSIZ ...

  4. [刷题] PTA 查验身份证

    题目: 7-63 查验身份证 (15 分)  一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...

  5. [刷题] PTA 04-树4 是否同一棵二叉搜索树

    程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct TreeNode *Tree; 4 struct ...

  6. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  7. [刷题] PTA 02-线性结构3 Reversing Linked List

    链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...

  8. [刷题] PTA 7-61 找最长的字符串

    程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 81 4 5 int main() { 6 char ch[N ...

  9. [刷题] PTA 7-62 切分表达式 写个tokenizer吧

    我的程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 50 4 char token[]= {'+','-',' ...

随机推荐

  1. electron踩坑系列之一

    前言 以electron作为基础框架,已经开发两个项目了.第一个项目,我主要负责用react写页面,第二项目既负责electron部分+UI部分. 做项目,就是踩坑, 一路做项目,一路踩坑,坑多不可怕 ...

  2. Windows下C++/Fortran调用.exe可执行文件

    目录 软件环境 Windows下CMake编译配置 设置项目的generator Command Line CMake GUI PreLoad.cmake 设置make 示例程序 CMake 设置Fo ...

  3. Istio 故障注入之延时(fixedDelay)

    Istio 故障注入 Istio 故障注入与其他在网络层引入错误(例如延迟数据包或者直接杀死 Pod)的机制不同,Istio 允许在应用程序层注入故障.这使得可以注入更多相关的故障,比如 HTTP 错 ...

  4. Kubernetes工作流程--<1>

    Kubernetes工作流程 客户端创建pod 流程: 用户管理员创建 Pod 的请求默认是通过kubectl 客户端管理命令 api server 组件进行交互的,默认会将请求发送给 API Ser ...

  5. 配置IIS虚拟站点(5)

    开发完ASP.NET网站后,想要直接浏览,不通过开发环境,那么就需要配置IIS虚拟站点 1.开始菜单->控制面板->所有控制面板->管理工具->双击打开Internet信息服务 ...

  6. Dynamics CRM实体系列之字段

    本节开始讲实体中的基础数据存储对象,也就是字段. Dynamics CRM目前总共有13种字段类型,分别为单行文本.选项集.多选选项集.两个选项.图像.整数.浮点数.十进制数.货币.多行文本.日期和时 ...

  7. SQL Server如何将查询的内容保存到新的sql 表中

    我是采用语句将 查询后的数据保存到一个新表中 1)采用into table 语句,需要保存到的这个新表不需要提前创建 select *into NewTable from Table --插入新表的语 ...

  8. NDEBUG与assert

    当宏NDEBUG定义在assert的头文件之前,会使assert.trace这类调试函数失效, 需要注意的是#define NDEBUG必须放在这些函数的头文件之前,放在它们的 头文件后面的话就相当于 ...

  9. redhat7.6 更换 centos7 YUM

    使用yum 遇到如下错误. This system is not registered to Red Hat Subscription Management. You can use subscrip ...

  10. (十八)VMware Harbor 镜像同步

    为什么需要镜像同步 由于对镜像的访问是一个核心的容器概念,在实际使用过程中,一个镜像库可能是不够用的,下例情况下,我们可能会需要部署多个镜像仓库: 国外的公有镜像下载过慢,需要一个中转仓库进行加速 容 ...