zoukankan      html  css  js  c++  java
  • 03.树2 List Leaves [层序遍历+建树]

    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
    

    #include<iostream>
    #include<queue>
    #define max 100 
    #define NULL -1
    using namespace std;
    int nn;
    struct node {
    	int left;
    	int right;
    }tree1[max];            //静态链表的使用 
    int create(struct node tree[],int n) {
    	int i,root = NULL;
    	scanf("%d", &n);
    	nn = n;
    	int* arr = new int[n + 1];
    	fill(arr, arr + n, 0);
    	char ch1, ch2;          //左子节点 右子节点输入 用char类型因为含有'-'; 
    	for (i = 0; i < n; i++) {                 //利用没有父节点指向根节点,找出根节点
    		getchar();           ///
    		scanf("%c %c", &ch1, &ch2);
    		if (ch1 != '-') {
    			tree[i].left = ch1 - '0';
    			arr[tree[i].left] = 1;
    		}
    		else tree[i].left = NULL;
    		if (ch2 != '-') {
    			tree[i].right = ch2 - '0';
    			arr[tree[i].right] = 1;
    		}
    		else tree[i].right = NULL;
    	}
    	for (i = 0; i < n; i++) {
    		if (!arr[i]) {
    			root = i;
    			break;
    		}
    	}
    	delete[]arr;
    	return root;
    }
    void levelorder(int root) {
    	queue<int>q;
    	int i;
    	int num = 0;
    	q.push(root);
    	while (!q.empty()) {
    		num++;
    		i = q.front();
    		q.pop();             //queue 和 stack的pop没有返回值的 
    		if (tree1[i].left != NULL && tree1[i].right != NULL) {
    			q.push(tree1[i].left);
    			q.push(tree1[i].right);
    		}
    		else if (tree1[i].left != NULL && tree1[i].right == NULL) {
    			q.push(tree1[i].left);
    		}
    		else if (tree1[i].left == NULL && tree1[i].right != NULL) {
    			q.push(tree1[i].right);
    		}
    		else {
    			if (num != nn) printf("%d ", i);
    			else printf("%d", i);
    		}
    	}
    }
    int main() {
    	int root = create(tree1,nn);
    	levelorder(root);
    	return 0;
    }
    
    
  • 相关阅读:
    我的vim开发环境搭建:C/C++/Go,持续更新中
    MFC的组合框(ComboBox)控件切换下拉样式
    回顾下杂乱的10月
    C++将整型数据转换成大端或小端存储顺序
    C/C++动态分配连续空间,下标越界导致的free():invalid next size问题
    O(n)空间复杂度,打印杨辉三角形的前n行
    C指针笔试题,蛋疼的多重指针运算,谭浩强的阴影
    2017滴滴出行笔试题:异或和为0的最大区间个数
    manjaro安装
    关于top命令
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13109985.html
Copyright © 2011-2022 走看看