PAT003 List Leaves

Source: Internet
Author: User

Topic:

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

Input Specification:

Each input file contains the one test case. For each case, the first line gives a positive integer N (<=10) which was the total number of nodes in the tree--and H Ence The nodes is numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the "the" and right children of the node. If the child does not exist, a "-" 'll be put on the position. Any pair of children is separated by a space.

Output Specification:

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

Sample Input:

Bayi---0-2 7----5-4 6

Sample Output:

4 1 5


Analysis: There are two main steps, one is to create a tree based on the input data, and the other is to sequence the tree traversal. A queue created through a linked list is used.

Code (c):
#include <stdio.h>typedefstructListNode {intvalue; CharLeftindex, Rightindex; structListNode *Left ; structListNode *Right ; structListNode *next;//Queue with} listnode;typedefstructQueue {ListNode*Front; ListNode*Rear; } Queue; Queue*Createqueue () {Queue*queue = (Queue *)malloc(sizeof(Queue)); Queue->front =NULL; Queue->rear =NULL; returnqueue;}voidAddtoqueue (Queue *queue, ListNode *node) {    if(! (queue->rear)) {Queue->rear =node; } Else{Queue->rear->next =node; Queue->rear =node; }        if(! (queue->front)) {Queue->front =node; }}listnode*deletefromqueue (Queue *queue) {ListNode*temp = queue->Front; if(temp) {Queue->front = queue->front->Next; returntemp; } Else {        returnNULL; }}intIsemptyqueue (Queue *queue) {    if(Queue->front = =NULL) {        return 1; } Else {        return 0; }}//List LeavesintMain () {//Accept Input    intNodecount; scanf ("%d", &nodecount);    ListNode A[nodecount]; intsum =0;  for(inti =0; i < Nodecount; i++) {        CharLeftindex, Rightindex; GetChar (); //remove the extra ' \ n 'scanf"%c%c", &leftindex, &Rightindex); ListNode input= * (ListNode *)malloc(sizeof(ListNode)); Input.leftindex=Leftindex; Input.rightindex=Rightindex; Input.left=NULL; Input.right=NULL; Input.value=i; A[i]=input; Sum+ = (Leftindex = ='-'?0: Leftindex-'0') + (Rightindex = ='-'?0: Rightindex-'0'); }        //Achievements     for(inti =0; i < Nodecount; i++) {ListNode*node = &(A[i]); CharLeftindex = node->Leftindex; CharRightindex = node->Rightindex; Node->left = leftindex! ='-'? & (A[leftindex-'0']): NULL; Node->right = rightindex! ='-'? & (A[rightindex-'0']): NULL; }    //root node subscript    intRootindex = (Nodecount-1) * Nodecount/2-sum; ListNode*root = &A[rootindex]; //Hierarchical traversal encounters leaf node outputQueue *queue =Createqueue ();    Addtoqueue (queue, root); intFlag =1;  while(!isemptyqueue (queue)) {ListNode*node =deletefromqueue (queue); if(! (node->left) &&! (node->Right )) {            if(flag) {printf ("%d", node->value); Flag=0; } Else{printf ("%d", node->value); }        }        if(node->Left ) {addtoqueue (Queue, node-Left ); }        if(node->Right ) {addtoqueue (Queue, node-Right ); }    }}

Operation Result:

PAT003 List Leaves

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.