1020. Tree Traversals (25)
来源:互联网 发布:机械加工编程软件 编辑:程序博客网 时间:2024/04/28 22:05
1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:72 3 1 5 7 6 41 2 3 4 5 6 7Sample Output:
4 1 6 3 5 7 2
#include <stdio.h>#include <stdlib.h>typedef struct node{int value;struct node *LeftNode;struct node *RightNode;} NODE;typedef struct queue{NODE *p;struct queue *next;} QUEUE;NODE * Rebuild(int *PostOrder,int *InOrder,int NodeNumber){NODE* Root;int i,RootPos;if(PostOrder==NULL || InOrder==NULL || NodeNumber==0){return NULL;}Root = (NODE*)malloc(sizeof(NODE));Root->value = PostOrder[NodeNumber-1]; //后序的最后一个为rootRoot->LeftNode = NULL;Root->RightNode = NULL;for(i=0;i<NodeNumber;i++) //查找中序中的root{if(InOrder[i] == Root->value){RootPos = i;break;}}Root->LeftNode = Rebuild(PostOrder,InOrder,RootPos);Root->RightNode = Rebuild(PostOrder+RootPos,InOrder+RootPos+1,NodeNumber-RootPos-1);return Root;}void LevelTravel(NODE* Root){QUEUE *queue, *t,*t1;NODE *visit,*copy;int flag = 0;if(Root == NULL){return;}queue = (QUEUE*)malloc(sizeof(QUEUE));copy = (NODE*)malloc(sizeof(QUEUE));copy->LeftNode = Root->LeftNode;copy->RightNode = Root->RightNode;copy->value = Root->value;queue->p = copy;queue->next = NULL;t = queue;while(queue != NULL){visit = queue->p;if(flag){printf(" %d",visit->value);}else{printf("%d",visit->value);flag = 1;}if(visit->LeftNode!=NULL){copy = (NODE*)malloc(sizeof(QUEUE));copy->LeftNode = visit->LeftNode->LeftNode;copy->RightNode = visit->LeftNode->RightNode;copy->value = visit->LeftNode->value;t1 = (QUEUE*)malloc(sizeof(QUEUE));t1->p = copy;t1->next = NULL;t->next = t1;t = t1;}if(visit->RightNode!=NULL){copy = (NODE*)malloc(sizeof(QUEUE));copy->LeftNode = visit->RightNode->LeftNode;copy->RightNode = visit->RightNode->RightNode;copy->value = visit->RightNode->value;t1 = (QUEUE*)malloc(sizeof(QUEUE));t1->p = copy;t1->next = NULL;t->next = t1;t = t1;}queue = queue->next;}return;}int main(){int N,i;int PostOrder[40];int InOrder[40];NODE * root;scanf("%d",&N);for(i=0;i<N;i++){scanf("%d",&PostOrder[i]);}for(i=0;i<N;i++){scanf("%d",&InOrder[i]);}root = Rebuild(PostOrder,InOrder,N);LevelTravel(root);//system("pause");return 0;}
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)
- hadoop virtual memory limits
- 《核心动画——关键帧动画》
- 151. Reverse Words in a String
- Zobrist 哈希
- oracle手动创建表空间
- 1020. Tree Traversals (25)
- XCodebuild
- STM32学习之启动代码注释
- ElasticSearch API 之 GET
- 拖拽自动排序插件
- js 字符串与数值相互转换,以及时间戳转换成日期形式字符串方法
- SSM框架的详细解说
- Handler内存泄漏
- datagrid getselected/getselections/getData之间的用法差异