Description:
-
Each node of the binary tree is printed from top to bottom, and the nodes at the same layer are printed from left to right.
-
Input:
-
The input may contain multiple test examples. The input ends with EOF.
For each test case, the input first line is an integer n (1 <=n <= 1000, where n represents the number of Binary Tree Elements to be input (nodes start from 1 ). The next row contains n numbers, representing the value of the element of the I-th Binary Tree node. Next there are n rows, each row has a letter Ci.
Ci = 'D' indicates that the I node has two children, followed by the left and right children numbers.
Ci = 'l' indicates that the I node has a left child followed by the ID of the left child.
Ci = 'R' indicates that the I node has a right child followed by the number of the right child.
Ci = 'Z' indicates that no child exists on the I node.
-
Output:
-
Corresponding to each test case,
Print the value of the Binary Tree node from the left to the right according to the top.
-
Sample input:
78 6 5 7 10 9 11d 2 5d 3 4zzd 6 7zz
-
Sample output:
8 6 10 5 7 9 11
Solution:
This is a classic breadth-first algorithm.
Here is the breadth-first algorithm.
Algorithm idea:
1. Scan the top-level tree node and put its child node into the queue.
2. Each time the first node of the queue is scanned, the child of the queue is still added to the queue, and the left child is followed by the right child. This ensures the order of the left and right layers.
3 until the queue is empty.
// Code findTree (a, n, 1) in main (); while (begin_q! = End_q) {findTree (a, n, Quene [begin_q ++]);} // scan function 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 1005typedef 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****************************************************************/