PAT 1020. Tree Traversals
来源:互联网 发布:mac系统打符号 编辑:程序博客网 时间:2024/05/17 16:46
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:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
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:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
题解:
#include <iostream>#include <vector>#include <deque>#include <algorithm>using namespace std;class node{public:int data;node* left;node* right;node(int v):data(v),left(NULL),right(NULL){}};node* maketree(node* root,vector<int> &postorder,vector<int> inoder)//建树{if(inoder.empty())return NULL;int value=postorder.back();postorder.pop_back();root=new node(value);vector<int>::iterator itr,mid;vector<int> left_in;vector<int> right_in;mid=find(inoder.begin(),inoder.end(),value);//找到postorder最后一个在inorder中的位子for(itr=inoder.begin();itr<mid;itr++)//左子树left_in.push_back(*itr);for(itr=mid+1;itr<inoder.end();itr++)//右子树right_in.push_back(*itr);root->right=maketree(root->right,postorder,right_in);root->left=maketree(root->left,postorder,left_in);return root;}void levelprint(deque<node> &result)//层序打印{if(result[0].left!=NULL)//将即将打印的node的左右子树依次push_back()result.push_back(*result[0].left);if(result[0].right!=NULL)result.push_back(*result[0].right);if(result.size()==1){cout<<result[0].data;return; //打印完毕}cout<<result[0].data<<" ";result.pop_front();levelprint(result);return;}int main(){int n;cin>>n;vector<int> postorder(n);vector<int> inorder(n);node* root=NULL;int i;for(i=0;i<n;i++)cin>>postorder[i];for(i=0;i<n;i++)cin>>inorder[i];root=maketree(root,postorder,inorder);deque<node> result;//创建打印队列result.push_back(*root);levelprint(result);return 0;}
0 0
- 【PAT】1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT--1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT 1020. Tree Traversals
- 【PAT】1020. Tree Traversals
- pat 1020. Tree Traversals (25)
- 【PAT】1020. Tree Traversals (25)
- PAT: 1020. Tree Traversals (25)
- PAT 1020. Tree Traversals(25)
- 【PAT】1020. Tree Traversals (25)
- [PAT]1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)
- pat 1020. Tree Traversals (25)
- PAT 1020. Tree Traversals (25)
- pat 1020. Tree Traversals (25)
- 谈谈数据库的字段设计的几个心得
- 畅通工程
- MySQL DATE_SUB() 函数
- 写代码学习Windows程序编写---动态链接库编程
- Mysql的row_format
- PAT 1020. Tree Traversals
- 电脑键盘部分按键失灵的解决方法
- poj 2492 A Bug's Life
- 关于Linux 64位安装hadoop2.2.0报异常的解决方法
- Dedecms5.7修改TAG、title、keywords及栏目名称长度限制
- 详解Mysql中InnoDB和MyISAM两种表类型的区别1
- MySQL中的Rows,Row_format以及MyISAM和InnoDB的一点区别
- MySQL-索引结构详解
- 用本地搭建Web服务器遇到的IP地址的问题