[刷题] PTA 03-树2 List Leaves
程序:
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的更多相关文章
- [刷题] PTA 03-树1 树的同构
程序: 1 #include <stdio.h> 2 #define MaxTree 10 3 #define ElementType char 4 #define Tree int 5 ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
- [刷题] PTA 03-树3 Tree Traversals Again
用栈实现树遍历 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXSIZE 30 4 5 int Pre[MAXSIZ ...
- [刷题] PTA 查验身份证
题目: 7-63 查验身份证 (15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...
- [刷题] PTA 04-树4 是否同一棵二叉搜索树
程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct TreeNode *Tree; 4 struct ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- [刷题] PTA 02-线性结构3 Reversing Linked List
链表逆序 1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namesp ...
- [刷题] PTA 7-61 找最长的字符串
程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 81 4 5 int main() { 6 char ch[N ...
- [刷题] PTA 7-62 切分表达式 写个tokenizer吧
我的程序: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 50 4 char token[]= {'+','-',' ...
随机推荐
- Android Studio中Switch控件有关 thumb 和 track 用法
•任务 •属性 android:track:底部的图片(灰->绿) android:thumb:设置 Switch 上面滑动的滑块,也就是上图中的白色圆形滑块 •switch_thumb 点击 ...
- 第22 章 : 有状态应用编排 StatefulSet
有状态应用编排 StatefulSet 本文将主要分享以下四方面的内容: "有状态"需求 用例解读 操作演示 架构设计 "有状态"需求 课程回顾 我们之前讲到过 ...
- Prometheus 配置文件中 metric_relabel_configs 配置--转载
Prometheus 配置文件中 metric_relabel_configs 配置 参考1:https://www.baidu.com/link?url=YfpBgnD1RoEthqXOL3Lgny ...
- [2020年10月28日普级组]1408.MSWORLD
1408. M S W O R L D 1408.MSWORLD 1408.MSWORLD 题目描述 Bessie , Farmer John 的优选牛,刚刚获得了一个牛科动物选美比赛的冠军!并得到了 ...
- 滴水逆向初级-C语言(二)
2.1.C语言的汇编表示 c语言代码 int plus(int x,int y) { return 0; } void main() { __asm { mov eax,eax } //调用函数 pl ...
- (十六)Struts2的标签库
一.简介 Struts2的标签库使用OGNL为基础,大大简化了数据的输出,也提供了大量标签来生成页面效果,功能非常强大. 在早期的web应用开发中,jsp页面主要使用jsp脚本来控制输出.jsp页面嵌 ...
- 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)
1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...
- 【CTF】图片隐写术 · 修复被修改尺寸的PNG图片
前言 今天我们想来介绍一下关于图片隐写相关处理,以及修复被修改尺寸的PNG图片. 关于PNG图片的相关处理,是CTF Misc图片隐写术中极为基础的一项操作,笔者这里是想要提一些做题过程中发现的小技巧 ...
- Manjaro 安装教程
1 概述 本文讲述了如何在单硬盘下对Manjaro进行安装. 2 写U盘 首先第一步是下载镜像,官网下载地址戳这里,如果下载速度慢可以选择国内镜像,比如戳这里. 笔者选择的是XFCE桌面: 下载好后将 ...
- 统计学习方法——实现AdaBoost
Adaboost 适用问题:二分类问题 模型:加法模型 \[f(x)=\sum_{m=1}^{M} \alpha_{m} G_{m}(x) \] 策略:损失函数为指数函数 \[L(y,f(x))=ex ...