zoukankan      html  css  js  c++  java
  • List Leaves

    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

    我的代码:C++(g++6.5.0)

    #include <iostream>
    #define Node int
    #define MaxNodeNum 10
    
    typedef struct Queue *PtrToQueue;
    struct Queue {
    	int Front;
    	int Rear;
    	int maxSize;
    	Node Arr[MaxNodeNum + 1]; //留一空间不存储数据,仅用于判断是否为空
    };
    typedef PtrToQueue Que;
    
    Que createQue() { //用循环数组表示队列
    	Que Q = (Que)malloc(sizeof(struct Queue));  //建立队列
    	Q->maxSize = MaxNodeNum + 1;
    	Q->Front = 0;
    	Q->Rear = 0;
    	return Q;
    }
    
    bool QueIsFull(Que Q) {
    	if ((Q->Rear + 1) % Q->maxSize == Q->Front) return true;
    	else return false;
    }
    bool QueIsEmpty(Que Q) {
    	if (Q->Rear == Q->Front) return true;
    	else return false;
    }
    void EnterQue(Que Q, Node V) {
    	if (QueIsFull(Q)) return;
    	Q->Rear = (Q->Rear + 1) % Q->maxSize;
    	Q->Arr[Q->Rear] = V;
    }
    Node DelQue(Que Q) {
    	if (QueIsEmpty(Q)) return -1;
    	Q->Front = (Q->Front + 1) % Q->maxSize;
    	return Q->Arr[Q->Front];
    }
    
    typedef struct treeNode *PtrTreeNode;
    struct treeNode {
    	Node left;
    	Node right;
    };
    typedef PtrTreeNode Tree;
    
    bool IsRoot[MaxNodeNum]; //全局数组,查找根节点时使用
    
    Tree buildTree(int N, bool IsRoot[]) {  //构建树,返回根节点
    	if (N <= 0) return NULL; //空树
    	int i;
    	char left, right;
    	Tree T = (PtrTreeNode)malloc(N*(sizeof(struct treeNode))); //用一维数组存储树节点
    	for (i = 0; i < MaxNodeNum; i++) IsRoot[i] = true;  //false表示该节点有被指,非根节点
    	for (i = 0; i < N;i++) {
    		scanf("%c %c
    ", &left, &right);
    		if (left == '-') {
    			T[i].left = -1;
    		}else {
    			T[i].left = left - '0';
    			IsRoot[T[i].left] = false;
    		}
    		if (right == '-') {
    			T[i].right = -1;
    		}
    		else {
    			T[i].right = right - '0';
    			IsRoot[T[i].right] = false;
    		}
    	}
    	return T;
    }
    Node srchRootNode(Tree T, int N, bool IsRoot[]) { //返回树的根节点
    	if (T == NULL) return -1;
    	int i;
    	Node RootNode;
    	for (i = 0; i < N; i++) {
    		if (IsRoot[i] == true) {
    			RootNode = i;
    			break;
    		}
    	}
    	return RootNode;
    }
    
    void leaves(Tree T,Node RootNode, Que Q) { //按照层序遍历顺序,输出叶节点
    	Node temp;
    	bool frt = true; //第一个输出叶节点的标记位 
    	if (RootNode == -1) return;
    	EnterQue(Q, RootNode);
    	while (!QueIsEmpty(Q)) {
    		temp = DelQue(Q);
    		if (T[temp].left == -1 && T[temp].right == -1) {
    			if (frt) { 
    				printf("%d", temp);
    				frt = false;
    			}else { printf(" %d", temp); }
    		}else {
    			if (T[temp].left != -1) EnterQue(Q, T[temp].left);
    			if (T[temp].right != -1) EnterQue(Q, T[temp].right);
    			}
    	}
    }
    
    int main()
    {
    	int N;
    	Que Q = createQue();
    	scanf("%d
    ",&N);
    	Tree T = buildTree(N, IsRoot);
    	Node RootNode = srchRootNode(T, N, IsRoot);
    	leaves(T, RootNode, Q);
        free(T); //malloc 对应 free
    	return 0;
    }
    
  • 相关阅读:
    <JavaScript> 组合继承
    <JavaScript> 稳妥构造函数模式与工厂模式的区别
    <JavaScript> call()、apply()、bind() 的用法
    <JavaScript>可枚举属性与不可枚举属性
    <JavaScript>闭包(closure)
    在MongoDB中实现聚合函数
    (转)如何入门 Python 爬虫
    Python爬虫实战四之抓取淘宝MM照片
    转载:十年驾车经验总结:活着,才是硬道理
    设计模式之单例模式的七种写法
  • 原文地址:https://www.cnblogs.com/TangYJHappen/p/13624258.html
Copyright © 2011-2022 走看看