当前位置: 首页 > news >正文

03-树2 List Leaves(浙大数据结构PTA习题)

03-树2 List Leaves

分数 25        全屏浏览        切换布局        作者 陈越        单位 浙江大学

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5

代码长度限制:16 KB        时间限制:400 ms        内存限制:64 MB

题目解析:

题目大意:给定一系列树的结点,按照从上到下,从左到右的顺序输出这棵树的叶子结点

关键点1:找到树的根结点

        

关键点2:根据树的根结点以及其它一系列结点构建树

         采用递归的思路,如果根结点的左子树不为空,则递归构建左子树,否则为NULL;如果根结点的右子树不为空,则递归构建右子树,否则为NULL;

关键点3:按照从上到下,从左到右的顺序输出这棵树的叶子结点

        本质是树的层序遍历问题,借助队列实现;

参考代码:

# include<stdio.h>
# include<stdbool.h>
# include<stdlib.h>
# define MAXNODE 10 typedef char ElementType;typedef struct TreeNode* Tree;
struct TreeNode{ElementType info,left,right;Tree Left;Tree Right;
};Tree Create(); 
Tree InitialTree(Tree Array[],int N);
Tree CreateTree(Tree Array[],Tree Root);
void InOrderPrint(Tree tree);int main(){// 根据输入创建树 Tree tree = Create();// 对这棵树进行层序遍历,并输出叶子结点InOrderPrint(tree); return 0;
}// 对一棵树进行层序遍历,并输出叶子结点
void InOrderPrint(Tree tree){// 创建一个结点指针队列	Tree Queue[MAXNODE];int Rear=-1, Head = -1;// 压入根结点Queue[++Rear] = tree;// 用于格式化输出的统计int count = 0; while(Rear!=Head){// 从队列出队一个结点,并判读是否是叶节点 Tree tmp = Queue[++Head];if(tmp->Left == NULL && tmp->Right==NULL){if(count==0){printf("%d",tmp->info-'0');count++;}else printf(" %d",tmp->info-'0');}// 分别压入其左右结点(若不为空)if(tmp->Left)Queue[++Rear] = tmp->Left;if(tmp->Right)Queue[++Rear] = tmp->Right; } return;} // 根据输入创建一棵树
Tree Create(){int N;scanf("%d",&N);getchar();if(N==0)return NULL;Tree Array[N];// 将信息存入指针数组中并获得树的根结点Tree Root = InitialTree(Array,N);// 通过树的根结点来建立树 Tree tree = CreateTree(Array,Root);return tree; 
} // 将结点信息存入指针数组中,并返回树的根结点 
Tree InitialTree(Tree Array[],int N){// Check数组用来标记结点是否作为了子节点 int i,Check[N];for(i=0;i<N;i++)Check[i] = 1;// 读入结点信息,并判读每个结点是否作为了子节点 for(i=0;i<N;i++){Tree node = (Tree)malloc(sizeof(struct TreeNode));node->info = '0'+i;node->left = getchar();if(node->left!='-')Check[node->left-'0'] = 0;getchar();node->right = getchar();if(node->right!='-')Check[node->right-'0'] = 0;getchar();node->Left = node->Right = NULL;Array[i] = node;}for(i=0;i<N;i++){if(Check[i]==1)break;}return Array[i];
}// 根据指针数组及根结点递归构建一棵树
Tree CreateTree(Tree Array[],Tree Root){int i,count;// 递归构建左子树 if(Root->left == '-'){Root->Left = NULL;}else{Root->Left = CreateTree(Array,Array[Root->left-'0']);}// 递归构建右子树if(Root->right == '-'){Root->Right = NULL;}else{Root->Right = CreateTree(Array,Array[Root->right-'0']);}return Root;
} 

运行结果:

相关文章:

  • C语言分支和循环(2)
  • vue路由跳转之【编程式导航与传参】
  • 万界星空科技MES系统功能介绍
  • 接口基础知识 工具使用
  • 使用 Vue 3 和 qrcode.js 开发二维码显示组件
  • Java 抽象类和接口
  • C++学习第十一天——vector的模拟实现
  • CSS-in-JS学习
  • 实验报告2-多线程并发
  • latex中对目录的处理
  • C++的算法:贪心算法
  • 单片机通信协议(1):SPI简介
  • 前端从零到一开发vscode插件并发布到插件市场
  • 如何在 JavaScript 中快速读取文件
  • ****三次握手和四次挥手
  • 2017届校招提前批面试回顾
  • Cumulo 的 ClojureScript 模块已经成型
  • JavaScript服务器推送技术之 WebSocket
  • Java到底能干嘛?
  • MYSQL 的 IF 函数
  • rc-form之最单纯情况
  • Selenium实战教程系列(二)---元素定位
  • Zepto.js源码学习之二
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 第十八天-企业应用架构模式-基本模式
  • 机器学习学习笔记一
  • 解析 Webpack中import、require、按需加载的执行过程
  • 开发基于以太坊智能合约的DApp
  • 世界上最简单的无等待算法(getAndIncrement)
  • 数组大概知多少
  • 在electron中实现跨域请求,无需更改服务器端设置
  • 06-01 点餐小程序前台界面搭建
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • # 数论-逆元
  • #NOIP 2014# day.2 T2 寻找道路
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (70min)字节暑假实习二面(已挂)
  • (阿里云在线播放)基于SpringBoot+Vue前后端分离的在线教育平台项目
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (六)软件测试分工
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (译)计算距离、方位和更多经纬度之间的点
  • (转)nsfocus-绿盟科技笔试题目
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
  • .gitignore文件使用
  • .net CHARTING图表控件下载地址
  • .Net IOC框架入门之一 Unity
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • .NET轻量级ORM组件Dapper葵花宝典
  • ??myeclipse+tomcat