【PAT】1020. Tree Traversals (25)

来源:互联网 发布:网络推广服务 编辑:程序博客网 时间:2024/06/05 07:54

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 7
Sample Output:
4 1 6 3 5 7 2


分析:考察树的建立和遍历。

可参考《编程之美》3.9


#include<iostream>#include<map>#include<vector>#include<queue>using namespace std;struct Node{Node *left;Node *right;int value;Node():left(NULL),right(NULL){}};void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){//判断何时结束递归if(PostOrder == NULL || InOrder == NULL){root = NULL;return ;}if(root == NULL) root = new Node;root->value = *(PostOrder + len - 1);root->left = NULL;root->right = NULL;if(len == 1) return;int count = 0;int *temp = InOrder;while(*temp != *(PostOrder + len -1)){count ++;temp++;if(count > len) break;}int left = temp - InOrder ;int right = len - left - 1;if(left > 0)Rebuild(PostOrder, InOrder, left, root->left);if(right > 0)Rebuild(PostOrder + left, InOrder+left+1, right, root->right);}int main(){int n,i,t;while(cin>>n){int *PostOrder = new int[n];int *InOrder = new int[n];for(i=0; i<n; i++)cin>>PostOrder[i];for(i=0; i<n; i++)cin>>InOrder[i];Node *root = new Node;int post_start,in_start;post_start = 0;in_start = 0;Rebuild(PostOrder, InOrder, n, root);queue<Node *> q;q.push(root);int flag = 1;while(!q.empty()){if(q.front()->left != NULL)q.push(q.front()->left);if(q.front()->right != NULL)q.push(q.front()->right);if(flag != n)cout<<q.front()->value<<" ";else cout<<q.front()->value;flag ++;q.pop();}cout<<endl;}return 0;}


今天自己写了一个版本:

#include <iostream>#include <vector>#include <queue>using namespace std;int *postOrder;int *inOrder;struct node{node(){left = NULL;right = NULL;}int val;node *left;node *right;};node* build(int *postOrder, int *inOrder, int len, node*root){int val = postOrder[len-1];root->val = val;int i, leftLen=0;for(i=0; i<len; i++){leftLen++;if(inOrder[i] == val){break;}}if(leftLen-1>0){node *left = new node();left = build(postOrder, inOrder, leftLen-1, left);root->left = left;}else{root->left = NULL;}int rightLen = len-leftLen;if(rightLen>0){node *right = new node();right = build(postOrder+(leftLen-1), inOrder+leftLen, rightLen, right);root->right = right;}else{root->right = NULL;}return root;}int main(int argc, char** argv) {int n;scanf("%d",&n);postOrder = new int[n];inOrder = new int[n];int i;for(i=0; i<n; i++){scanf("%d",&postOrder[i]);}for(i=0; i<n; i++){scanf("%d",&inOrder[i]);}node *root = new node();root = build(postOrder,inOrder,n,root);queue<node> que;que.push(*root);vector<int> vec;while(!que.empty()){node tmp = que.front();vec.push_back(tmp.val);if(tmp.left!=NULL){que.push(*tmp.left);}if(tmp.right!=NULL){que.push(*tmp.right);}que.pop();}for(i=0; i<vec.size(); i++){if(i==0){printf("%d",vec[i]);}else{printf(" %d",vec[i]);}}printf("\n");return 0;}