程序:

 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. AttributeError: 'str' object has no attribute 'lowerr' Python常见错误

    方法名拼写错误 检查方法名拼写,如有错误改正即可 特别注意m和n

  2. windows环境下抓密码总结

    在线抓密码 1.mimikatz privilege::debug token::whoami token::elevate lsadump::sam mimikatz.exe "privi ...

  3. ECharts绘制折线图

    首先看实现好的页面 实现 首先引入echarts工具 // vue文件中引入echarts工具 let echarts = require('echarts/lib/echarts') require ...

  4. 北航OO第一单元作业总结(Retake)

    前言:当我写这篇博客的时候,我的心情是复杂的,因为这实际上是我第二次写这篇博客--我今年重修的这门课.我对去年的成绩心有不甘--在激烈的竞争下,我虽然尽可能完成了所有作业(仅一次作业未通过弱测),但爆 ...

  5. 【spring源码系列】之【xml解析】

    1. 读源码的方法 java程序员都知道读源码的重要性,尤其是spring的源码,代码设计不仅优雅,而且功能越来越强大,几乎可以与很多开源框架整合,让应用更易于专注业务领域开发.但是能把spring的 ...

  6. 配置动态刷新RefreshScope注解使用局限性(一)

    在 Spring Cloud 体系的项目中,配置中心主要用于提供分布式的配置管理,其中有一个重要的注解:@RefreshScope,如果代码中需要动态刷新配置,在需要的类上加上该注解就行.本文分享一下 ...

  7. 2.Python进程间的通信之队列(Queue)和生产者消费者模型

    一.队列 1.1 概念介绍-----multiprocess.Queue 创建共享的进程队列,Queue是多进程安全的队列,可以使用Queue实现多进程之间的数据传递. Queue([maxsize] ...

  8. kubernetes集群证书更新

    kubeadm 默认证书为一年,一年过期后,会导致api service不可用,使用过程中会出现:x509: certificate has expired or is not yet valid. ...

  9. 如何在linux上手动连接到互联网

    作者 @飞洲人飞舟魂转载请注明出处. 一直以来对linux的网络管理不大明白,今天研究了一下网络的手动配置,现在记录一下.我使用Ubuntu20.04.1来进行演示. 介绍 首先我们先明确一些概念性的 ...

  10. 02- HTML网页基础知识与浏览器介绍

    1.认识网页 网页主要由文字,图像和超链接等元素构成.当然,除了这些元素,网页还可以包含音频,视频,以及flask等. 如图所示就是一个网页: 网页是如何形成的呢? 它是由前端人员写的代码,经过浏览器 ...