程序:

 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. 力扣 - 剑指 Offer 37. 序列化二叉树

    目录 题目 思路 代码 复杂度分析 题目 剑指 Offer 37. 序列化二叉树 思路 序列化其实就是层序遍历 但是,要能反序列化的话,前.中.后.层序遍历是不够的,必须在序列化时候保存所有信息,这样 ...

  2. 解决“用PicGo-2.3.0-beta5 + GitHub做博客图床,github仓库图片文件不显示”的问题记录(备忘)

    解决"用PicGo-2.3.0-beta5 + GitHub做博客图床,github仓库图片文件不显示"的问题记录(备忘) 历时几个小时百度,终于靠自己理解解决了GitHub仓库图 ...

  3. (十四)struts2的国际化

    一.国际化的概念 国际化是指web程序在运行时,根据客户端请求的国家.语言的不同而显示不同的界面. 例如,如果请求来自中文客户端,则页面的显示,提示信息等都是中文,如果是英文客户端,则显示英文信息.  ...

  4. ES6转ES5(Babel转码器)

    ES6转ES5(Babel转码器) 前提:必须在VScode中已经安装了Node.js 官网:https://nodejs.org/en/ 一.安装命令行转码工具 npm install --glob ...

  5. gdb常用命令(转)

    pwn常常会用到gdb,看到一篇不错的文章,记录了很多命令:https://www.jianshu.com/p/c3e5f5972b21 gdb 基础调试命令 s step,si步入 n 执行下一条指 ...

  6. ret2dl64

    ret2dl64 ret2dl64 与ret2dl32不同,ret2dl64需要知道libc. 检查保护: IDA看一看 read_got 被置为0,强制你使用ret2dlresolve. 我们先伪造 ...

  7. Run Horovod

    Run Horovod https://horovod.readthedocs.io/en/stable/running_include.html https://horovod.readthedoc ...

  8. shellcode隐写到像素RGB免杀上线到CS

    利用把Shellcode隐写到图片像素RGB进行免杀上线到CS --by:chenw 0x01 前言 前几天跟一个朋友一起搞一个站的时候,发现那个站点开了很多杀软,使用CS的powershell马无法 ...

  9. 1.4.18- HTML之特殊字符标签

    上面图片内容简单了解一下,下面我们看一段代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. 【Vue】Vue学习(四)-状态管理中心Vuex的简单使用

    一.vuex的简介 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.Vuex背后的基本思想,就是前面所说的单向数据流.图4就是Vuex实现单向数据流的示意图.    Store     ...