【C/C++】List Leaves/层序遍历叶子结点
来源:互联网 发布:c语言平方根号怎么表示 编辑:程序博客网 时间:2024/06/05 18:10
7-4 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:
81 -- -0 -2 7- -- -5 -4 6
Sample Output:
4 1 5
题目源于:https://pintia.cn/problem-sets/16/problems/666
题目要求:通过一个结构数组建立一棵树,按照从上到下,从左到右的顺序,打印所有叶子结点。
思路分析:通过判断结构数组的left,right是否为‘-’,来判断是否是叶子结点,通过队列来层序遍历二叉树。
// ListLeaves.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#define MAXSIZE 10using namespace std;//template<class T> typedef struct Queue *queue;struct Node {int pos;char left;char right;Node() {pos = -1;left = -1;right = -1;}};typedef struct Node* node; struct Queue {struct Node data[MAXSIZE];int front;int rear;};typedef struct Queue* queue;//程序主要操作void input(struct Node * node, int N);void process(int * a, struct Node * node,int N);int find(struct Node * T,int N);//层序遍历int * levelOrder(struct Node * T, int root);//队列基本操作queue CreateQueue();struct Node deleteQueue(queue Q);void addQueue(queue Q,struct Node data);bool isEmpty(queue Q);bool isFull(queue Q);int main(){struct Node list[MAXSIZE];int N;//inputcin >> N;input(list, N);//processint *a = new int[N];for (int i = 0; i < N; i++) a[i] = 0;//记录叶子结点process(a, list,N);//outputint root = find(list, N);int sum=0;for (int i = 0; i < N; i++) { if (a[i] == 1) sum++; }//因为需要按照从上到下,从左到右的顺序打印,所以对对树进行层序遍历,按照遍历顺序依次打印叶子结点int *level = levelOrder(list, root);for (int i = 0; i < N && sum>1; i++) {if (a[level[i]] == 1) {cout << level[i] << ' ';sum--;}}for (int i = N - 1; i >= 0; i--) {if (a[level[i]] == 1) {cout << level[i];break;}}delete[] a; return 0;}//输入函数void input(struct Node * list, int N) {for (int i = 0; i < N; i++) {list[i].pos = i;cin >> list[i].left >> list[i].right;}}//寻找叶子结点void process(int * a, struct Node * node,int N) {for (int i = 0; i < N; i++) {if (node[i].left == '-'&&node[i].right == '-') a[i] = 1;}}//寻找树根int find(struct Node * T, int N) {int *a = new int[N];for (int i = 0; i < N; i++) a[i] = 0;for (int i = 0; i < N; i++) {if (T[i].left != '-') {int l = T[i].left - '0';a[l] = 1;}if (T[i].right != '-') {int r = T[i].right - '0';a[r] = 1;}}int i;for ( i = 0; i < N; i++) if (!a[i]) break;delete[] a;return i;}int integer(char a) {return a - '0';}//创建一个空的队列queue CreateQueue() {queue Q = new struct Queue;Q->front = -1;Q->rear = -1;return Q;}//出队struct Node deleteQueue(queue Q) {if (isEmpty(Q)) {cout << "Queue is empty!" << endl;}else{Q->front = Q->front + 1;//Q->data[Q->front] ;return Q->data[Q->front];}}//入队void addQueue(queue q, struct Node data) {if (isFull(q)) {cout << "Queue is full!" << endl;return;}else{q->rear = q->rear + 1;q->data[q->rear] = data;}}//判断队列是否为空bool isEmpty(queue Q) {if (Q->front == Q->rear) return true;else return false;}//判断队列是否为满bool isFull(queue Q) {if (Q->front == (Q->rear + 1) % MAXSIZE) return true;else return false;}//层序遍历int* levelOrder(struct Node *T, int root) {int * a = new int[MAXSIZE];int i = 0;queue q;q = CreateQueue();//q->data[1] = T[root];addQueue(q, T[root]);while (!isEmpty(q)) {struct Node tmp = deleteQueue(q);a[i++] = tmp.pos;if (tmp.left != '-'&&tmp.left != '-1') {int l = tmp.left - '0';addQueue(q, T[l]);}if (tmp.right != '-'&&tmp.right != '-1') {int r = tmp.right - '0';addQueue(q, T[r]);}}return a;}
非法
- 【C/C++】List Leaves/层序遍历叶子结点
- C语言二叉树创建、遍历以及统计结点、叶子结点
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- [LeetCode]404. Sum of Left Leaves(叶子结点的和)
- 【C】树的层序遍历
- 03-2. List Leaves (25)--c语言实现
- PAT 03-树1. List Leaves (25)(C)
- 03-树2. List Leaves (25) Python C
- C语言二叉树创建、遍历、深度、叶子节点
- 数据结构实验之二叉树七:叶子问题(层序遍历输出叶子)
- MSSQL 查找子结点与遍历叶子结点路径
- 03-树2 List Leaves (25分) (查找叶子节点)
- c:foreach遍历两个list
- c语言list嵌套遍历
- 数据结构--二叉树的创建、先序遍历、中序遍历、后序遍历、深度、叶子结点数
- 求根结点到每个叶子节点的逆序列【后序遍历非递归的应用】
- Depth-first Search -- Leetcode problem104. Maximum Depth of Binary Tree
- java文件下载中Content-disposition的定义
- Leetcode之Search in Rotated Sorted Array 问题
- Python
- 【NOIP2012】疫情控制
- 【C/C++】List Leaves/层序遍历叶子结点
- 第二十六篇 jQuery 学习8 遍历-父亲兄弟子孙元素
- Bayer Sensor OV13850
- Kotlin进阶之集合与区间
- LeetCode-Reverse Integer
- Javascript字符串模板简单实现(一)
- Angular + protractor + mocha
- Median of Two Sorted Arrays
- LintCode--first unique number in stream