程序:

 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. 【linux】命令-网络相关

    目录 前言 1. ifconfig 1.1 语法 1.2 参数说明 1.3 例程 2. iw 2.1 扫描可用无线网络 2.2 WiFi连接步骤(教程A) 2.2.1 查看可以用无线设备信息 2.2. ...

  2. 详解php中函数的引用传递和返回 (附代码)

    本篇文章带大家了解一下php的引用,详细介绍一下函数的引用传递和引用返回.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助.php的引用(就是在变量或者函数.对象等前面加上&符号 ...

  3. 「HTML+CSS」--自定义加载动画【010】

    前言 Hello!小伙伴! 首先非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 哈哈 自我介绍一下 昵称:海轰 标签:程序猿一只|C++选手|学生 简介:因C语言结识编程,随后转入计算机 ...

  4. [状压DP]子矩阵

    子 矩 阵 子矩阵 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵 如,下面左图中选取第 2 . 4 ...

  5. vs2019新建数据库后插入中文变问号

    在使用VS创建了数据库后如果直接给字符类型插入中文内容的话查询结果插入的中文会以"?"的格式展现. 原因是因为默认创建的数据库的排序类型为拉丁文不支持中文. 所以需要讲这个排序的字 ...

  6. VirtualBox CentOS8 调整分辨率

    1 概述 VirtualBox安装完CentOS8后无法调节分辨率,需要安装额外的工具. 2 安装依赖包 首先确保虚拟机能正常连接网络,然后安装:kernel.kernel-core.kernel-m ...

  7. Day07_37_深度剖析集合中的contains()方法

    深度剖析集合中的 contains()方法 contains()方法查找集合中是否包含某个元素 contains() 底层使用的是 equals()方法 当contains()方法拿到一个对象的时候, ...

  8. Cobalt-Strike Office宏利用与免杀

    1.打开Cobalt-Strike生产Office宏病毒. 首先需要设置监听器.因为钓鱼的目标比较单纯,在这里就不采用域前置技术. 然后使用攻击模块,生产Office宏病毒. 设置好监听器. 生成宏病 ...

  9. C++雾中风景17:模板的非推断语境与std::type_identity

    乍一看这个标题很玄乎,但是其实这只是涉及一个很简单的CPP的模板推导的知识点. 笔者近期进行CPP开发工作时,在编译时遇到了如下的模板类型的推断错误:note: candidate template ...

  10. 软件调研——GoodNotes 5与Notability

    项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任健) 这个作业的要求在哪里 作业要求 我在这个课程的目标是 积累软件开发经验,提高工程能力 这个作业在哪个具体方面帮助我实现目标 深入调 ...