PAT 1020 Tree Traversals(二叉の木の中の順序に従って、順番に階層を巡回する)
PAT 1020
1020 Tree Traversals (25分)
Suppose that all the keys in a binary tree are distinct positive integers.Gven the postorder and in order trversal sequences,you are supposed to out put the level order trure sal sequence of the corestinder.
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 e e e separated by space.
Output Specification:
For each test case,print in one line the level order trversal sequence of the cores ponding binary tree.All the numbers in a line must be separated by exactlyone space,and the must beのextra space space space the the the the the end of line.line.ine.ine.line.ine.ine.ine.line.ine.ine
Sample Input:
チェーンを変えてみましょう.針が本当に痛いです.
どのようにして既知の遍歴によって二叉樹を復元しますか?
テンプレートコードがあります.
詳しくはここに移す
1020 Tree Traversals (25分)
Suppose that all the keys in a binary tree are distinct positive integers.Gven the postorder and in order trversal sequences,you are supposed to out put the level order trure sal sequence of the corestinder.
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 e e e separated by space.
Output Specification:
For each test case,print in one line the level order trversal sequence of the cores ponding binary tree.All the numbers in a line must be separated by exactlyone space,and the must beのextra space space space the the the the the end of line.line.ine.ine.line.ine.ine.ine.line.ine.ine
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
#include
#include
#include
using namespace std;
typedef struct Bitree {
int data;
struct Bitree *left;
struct Bitree *right;
} tree,*Tree;
tree *Resume( int in[],int post[], int len) {
if(len<=0)
return NULL;
tree* temp=new tree;
temp->data=*post;
int i=0;
while(idata)
break;
i++;
}
temp->left=Resume(in,post-len+i,i);
temp->right=Resume(in+i+1,post-1,len-i-1);
return temp;
}
queueq;
int res[100000+50];
int len=0;
void dfs(Tree t) {
q.push(t);
while(!q.empty()) {
t=q.front();
q.pop();
res[len++]=t->data;
if(t->left!=NULL)
q.push(t->left);
if(t->right!=NULL)
q.push(t->right);
}
}
int in[100],post[100];
int main() {
int n;
cin>>n;
for(int i=0;i