OFFER prove safety of printing a binary tree from the top down (nine degrees OJ1523)

Subject description:

Downward from the print out of each node of the binary tree, with the layer node from left to right printing.

 

Input:

Input may contain multiple test samples, enter to EOF end.
For each test case, a first line of the input integer n (1 <= n <= 1000,: n represents the number of binary elements to be input (node number starting from 1) the next row of n-digits. value represents the i-th element of binary tree nodes. Then there are n rows, each row has a letter Ci of.
Ci of = 'D' denotes the i th child node has two children, followed by left child and right child ID number.
Ci = 'l' i denotes a left child node, followed by the number of children left.
CI = 'R & lt' denotes the i th node has a right child, right child followed number.
CI = 'Z 'denotes the i th child node has no children.

 

Output:

Corresponding to each test case,
the value of the binary tree node from below, from left to right in accordance with the print out.

 

Sample input:
7
8 6 5 7 10 9 11
d 2 5
d 3 4
z
z
d 6 7
z
z

 

Sample output:
8 6 10 5 7 9 11

Problem-solving ideas:

  The breadth-first algorithm is very classic, nothing to say.

  Breadth-first algorithm to see here

  Algorithm idea:

  1 Scan top-level tree nodes, it's child node in the queue.

  2 Scan the queue team head node, it is still a child added to the team, and in accordance with the child first left, then right child of the order, thus ensuring that the order of about one layer.

  3 until the queue is empty.

//main()中的代码 
   findTree(a,n,1);
        while(begin_q != end_q){
            findTree(a,n,Quene[begin_q++]);
        }
 
//扫描函数
void findTree(treeArr *a,int n,int j){
    if(j<=n){
        result[top_result++]=a->arr[j].num;
    }
    if(a->arr[j].lchild != 0){
        Quene[end_q++] = a->arr[j].lchild;
    }
    if(a->arr[j].rchild != 0){
        Quene[end_q++] = a->arr[j].rchild;
    }
}    

 

All Code:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define MAXSIZE 1005
typedef struct treenode{
    int num;
    int lchild;
    int rchild;
}Tree;
typedef struct treearr{
    struct treenode arr[MAXSIZE];
}treeArr;
 
int Quene[MAXSIZE]={0};
int begin_q,end_q;
int result[MAXSIZE]={0};
int top_result;
 
void findTree(treeArr *a,int n,int j);
 
int main(){
    int n,i;
    char c;
    int n1,n2;
    while(scanf("%d",&n)!=EOF && n>=1 && n<=1000){
        treeArr *a = (treeArr *)malloc(sizeof(treeArr));
 
        memset(&Quene,0,sizeof(int)*MAXSIZE);
        memset(&result,0,sizeof(int)*MAXSIZE);
         
        begin_q=0;
        end_q = 0;
        top_result = 0;
         
        for(i=0;i<MAXSIZE;i++){
            a->arr[i].num = 0;
            a->arr[i].lchild = 0;
            a->arr[i].rchild = 0;
        }
        for(i=1;i<=n;i++){
            scanf("%d",&a->arr[i].num);
        }
        for(i=1;i<=n;i++){
            scanf("\n%c",&c);
            if(c == 'd'){
                scanf("%d %d",&n1,&n2);
                a->arr[i].lchild = n1;
                a->arr[i].rchild = n2;
            }else if(c == 'l'){
                scanf("%d",&n1);
                a->arr[i].lchild = n1;
            }else if(c == 'r'){
                scanf("%d",&n1);
                a->arr[i].rchild = n1;
            }else{
 
            }
        }
        findTree(a,n,1);
        while(begin_q != end_q){
            //printf("findtree --- begin_q %d end_q %d\n",begin_q,end_q );
 
 
            findTree(a,n,Quene[begin_q++]);
        }
        for(i=0;i<n-1;i++){
            printf("%d ", result[i]);
        }
        printf("%d\n", result[n-1]);
    }
    return 0;
}
 
void findTree(treeArr *a,int n,int j){
    if(j<=n){
        result[top_result++]=a->arr[j].num;
    }
    if(a->arr[j].lchild != 0){
        Quene[end_q++] = a->arr[j].lchild;
    }
    if(a->arr[j].rchild != 0){
        Quene[end_q++] = a->arr[j].rchild;
    }
}
/**************************************************************
    Problem: 1523
    User: xhalo
    Language: C
    Result: Accepted
    Time:0 ms
    Memory:920 kb
****************************************************************/

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545353

Guess you like

Origin blog.csdn.net/weixin_34240657/article/details/91989537