Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder 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 (≤ 50,000), the total number of nodes in the binary tree. The second line gives the preorder 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 first number of the postorder traversal sequence of the corresponding binary tree.
Sample Input:
7
1 2 3 4 5 6 7
2 3 1 5 4 7 6
Sample Output:
3
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
struct node{
int val;
node* lchild, *rchild;
};
vector<int> preOrder;
vector<int> inOrder;
vector<int> postOrder;
unordered_map<int,int> hashTable;
node* buildTree(int preL, int preR, int inL, int inR){
if(preL > preR) return nullptr;
node* root = new node;
root->val = preOrder[preL];
root->lchild = root->rchild = nullptr;
int leftSubTree = hashTable[preOrder[preL]] - inL;
root->lchild = buildTree(preL+1,preL + leftSubTree,inL,inL+leftSubTree-1);
root->rchild = buildTree(preL+leftSubTree+1,preR,inL+leftSubTree+1,inR);
return root;
}
void postTravese(node* root){
if(root == nullptr) return;
postTravese(root->lchild);
postTravese(root->rchild);
postOrder.push_back(root->val);
}
int main(){
int n, x;
cin>>n;
for(int i = 0; i < n; i++){
cin>>x;
preOrder.push_back(x);
}
for(int i = 0; i < n; i++){
cin>>x;
inOrder.push_back(x);
}
for(int i = 0; i < n; i++){
hashTable[inOrder[i]] = i;//中序遍历每个结点代表的位置
}
node* root = nullptr;
root = buildTree(0,n-1,0,n-1);
postTravese(root);
cout<<postOrder[0];
return 0;
}
太简单了吧
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
vector<int> preOrder;
vector<int> inOrder;
unordered_map<int,int> ma;
struct node{
int val;
node* lchild, *rchild;
};
node* buildTree(int preL, int preR,int inL, int inR){
if(preL>preR) return nullptr;
node* root = new node;
root->val = preOrder[preL];
root->lchild = root->rchild = nullptr;
int subLeftTree = ma[preOrder[preL]] - inL;
root->lchild = buildTree(preL+1,preL+subLeftTree,inL,inL+subLeftTree-1);
root->rchild = buildTree(preL+subLeftTree+1,preR,inL+subLeftTree+1,inR);
return root;
}
vector<int> postOrder;
void postOrderTrave(node* v){
if(v==nullptr) return;
postOrderTrave(v->lchild);
postOrderTrave(v->rchild);
postOrder.push_back(v->val);
}
int main(){
int n;
cin>>n;
preOrder.resize(n+1);
inOrder.resize(n+1);
for(int i = 0;i < n; i++){
cin>>preOrder[i];
}
for(int i = 0; i < n; i++){
cin>>inOrder[i];
}
for(int i = 0; i < n; i++){
ma[inOrder[i]]=i;//记录中序序列每个值的下标
}
node* root = new node;
root = buildTree(0,n-1,0,n-1);
postOrderTrave(root);
cout<<postOrder[0];
return 0;
}
#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;
struct node{
int val;
node* lchild, *rchild;
};
vector<int> preOrder;
vector<int> inOrder;
vector<int> postOrder;
unordered_map<int,int> ma;
node* buildTree(int preL,int preR,int inL,int inR){
if(preL > preR) return nullptr;
node* root = new node;
root->val = preOrder[preL];
root->lchild = root->rchild = nullptr;
int subLeftTree = ma[preOrder[preL]] - inL;
root->lchild = buildTree(preL+1,preL+subLeftTree,inL,inL+subLeftTree-1);
root->rchild = buildTree(preL+subLeftTree+1,preR,inL+subLeftTree+1,inR);
return root;
}
void postTrave(node* root){
if(root == nullptr) return;
postTrave(root->lchild);
postTrave(root->rchild);
if(postOrder.size() == 1) return;
postOrder.push_back(root->val);
}
int main(){
int n;
cin>>n;
preOrder.resize(n+1);
inOrder.resize(n+1);
for(int i = 0; i < n; i++){
cin>>preOrder[i];
}
for(int i = 0; i < n; i++){
cin>>inOrder[i];
}
for(int i = 0; i < n; i++){
ma[inOrder[i]] = i;
}
node* root = new node;
root = buildTree(0,n-1,0,n-1);
postTrave(root);
cout<<postOrder[0];
return 0;
}