ツリー上の1127ジグザグ(30分)
バイナリツリー内のすべてのキーが明確な正の整数であると仮定します。一意のバイナリツリーは、後順の所与の対によって及びトラバーサル配列順序どおりに決定することができます。そしてレベル順に番号を印刷するには、単純な標準ルーチンです。あなたは問題があまりにもシンプルだと思う場合は、あなたはあまりにもナイーブです。つまり、ルートから開始し、右と左に右に左に交互に、数字レベルごとを印刷する - この時間は、あなたが「ジグザグ順」に番号を印刷することになっています。たとえば、次のツリーのあなたは出力しなければならない:1 11 5 8 17 12 20 15。
入力仕様:
各入力ファイルには、1つのテストケースが含まれています。各場合について、最初の行は、正の整数N(≤30)、二分木におけるノードの総数を与えます。2行目はINORDERシーケンスを与え、第三の線が後順のシーケンスを与えます。行のすべての数字は、スペースで区切られます。
出力仕様:
各テストケースのために、ラインのツリーのジグザグ配列を印刷します。行のすべての数字は正確に一つのスペースで区切らなければならず、行の最後に余分なスペースがあってはなりません。
サンプル入力:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
サンプル出力:
1 11 5 8 17 12 20 15
/ **
タイトルの質問が意図されています。
その後の木、行きがけ(ユニーク構築先行順木を推論することができる)、与えられた
このユニークなツリートラバーサルシーケンスの出力が、整形され、出力Z、EG:最初の右から左へ出力層は、第二層出力の左から右に
、このトピックやアイデア:方法
(ルートレイヤ0に位置する)添字インデックスノードのソート、第ソート高さ、右から左にも層(からを使用して昇順で(左から右へ、奇数層))大ソートする小
* /
/**
本题题意:
给出一颗树的后序,中序遍历(可以推导出先序遍历构建出唯一树),
输出此唯一树的层序遍历不过是z字型输出, eg: 第一层 从右往 左输出,第二层从左往右输出
本题思路
利用结点下标索引进行排序,先将高度排序,(根节点位于第0层)偶数层从右往左(从大到小排序,) 奇数层从左往右(从小到大排序)
**/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Node{
int h, index, data; //
};
int n;
vector<int> post, in;
vector<Node> zlevel;
bool cmp(Node a, Node b){
if(a.h != b.h)
return a.h < b.h;
else if(a.h % 2 == 0){ //当高度一致时,偶数层从右往左 (索引从大到小输出)
return a.index > b.index;
}else{ //奇数层,从左往右,(索引从 小 到 大 输出)
return a.index < b.index;
}
}
void preOrder(int postright, int inleft, int inright,int index, int h){
if(inleft > inright) return;
int i = inleft;
while(post[postright] != in[i]) i++;
// Node node = Node();//注意此步是 Node 没有new
// node.data = post[postright];
// node.h = h;
// node.index = index;
zlevel.push_back({h, index, post[postright]});
preOrder(postright - (inright - i) -1, inleft, i - 1, index * 2 + 1, h + 1);
preOrder(postright - 1, i + 1, inright, index * 2 + 2, h + 1);
}
int main(){
scanf("%d", &n);
in.resize(n);
post.resize(n);
for(int i = 0; i < n; i++){
scanf("%d", &in[i]);
}
for(int i = 0; i < n; i++){
scanf("%d", &post[i]);
}
preOrder(n - 1, 0, n - 1, 0, 0);
sort(zlevel.begin(), zlevel.end(), cmp);
for(int i = 0; i < n; i++){
if(i != 0)
printf(" ");
printf("%d", zlevel[i].data);
}
return 0;
}
方法2:
リストにリンクツリートラバーサル順序を確立するために、前文による打ち上げ後、レベルトラバース得BFSにより、高さは、二次元の結果アレイに従ってノードに格納されます。
偶数出力は逆の高さ(右から左へ)
高い正の奇数次出力(左から右へ)
特定のコード:
/**
思路 : 建立一颗树 将每层的结点通过BFS层序遍历分别放入二维向量数组 result[i]中(存放了在高度为i的全部结点)
当高度为偶数时输出 倒序输出
当高度为奇数时输出 正序输出
**/
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct Node{
Node *l, *r;
int data, h;
};
int n, cnt = 0; //cnt用来保证最后输出的格式
vector<int> post, in, result[35]; //
queue<Node *> q;
Node *preOrder(Node *root, int postright, int inleft, int inright, int h){
if(inleft > inright){
return nullptr;
}
int i = inleft;
while(post[postright] != in[i]) i++;
root = new Node();
root -> data = post[postright];
root -> h = h;
root -> l = preOrder(root -> l, postright -(inright - i) - 1, inleft, i - 1, h + 1);
root -> r = preOrder(root -> r, postright - 1, i + 1, inright, h + 1);
return root;
}
void levelOrder(Node *root){
q.push(root);
while(!q.empty()){
Node *n = q.front();
q.pop();
result[n->h].push_back(n->data);
if(n->l != nullptr)
q.push(n->l);
if(n->r != nullptr)
q.push(n->r);
}
}
int main(){
//k % 2 == 0表示从左至右遍历 k % 2 == 1 表示从右至左遍历
Node *root = nullptr;
scanf("%d", &n);
in.resize(n);
post.resize(n);
for(int i = 0; i < n; i++){
scanf("%d", &in[i]);
}
for(int i = 0; i < n; i++){
scanf("%d", &post[i]);
}
root = preOrder(root, n - 1, 0, n - 1, 0); //构建出一棵树
levelOrder(root); //进行层序遍历并就结点放入二维向量数组中
printf("%d", result[0][0]);
for(int i = 1; i < n; i++){
if(i % 2 == 1)
for(int j = 0; j < result[i].size(); j++)
printf(" %d", result[i][j]);
if(i % 2 == 0)
for(int j = result[i].size() - 1; j >= 0; j--)
printf(" %d", result[i][j]);
}
return 0;
}
もちろん、あなたがツリーリストの静的な配列表現を使用することはできません。
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct Node{
int lindex, rindex, h, index;
}node[35];
int n;
queue<Node> q;
vector<int> in, post, result[35];
int preOrder(int postright, int inleft, int inright, int h){
if(inleft > inright) return -1;
int i = inleft;
while(post[postright] != in[i]) i++;
node[postright].index = postright;
node[postright].h = h;
node[postright].lindex = preOrder(postright - (inright - i) - 1, inleft, i - 1, h + 1);
node[postright].rindex = preOrder(postright - 1, i + 1, inright, h + 1);
return postright;
}
void levelBFS(int postright){
q.push(node[postright]);
while(!q.empty()){
Node n = q.front();
result[n.h].push_back(post[n.index]);
q.pop();
if(n.lindex != -1){
q.push(node[n.lindex]);
}
if(n.rindex != -1){
q.push(node[n.rindex]);
}
}
}
int main(){
int n;
scanf("%d", &n);
in.resize(n);
post.resize(n);
for(int i = 0; i < n; i++){
scanf("%d", &in[i]);
}
for(int i = 0; i < n; i++){
scanf("%d", &post[i]);
}
preOrder(n - 1, 0, n - 1, 0);
levelBFS(n - 1);
printf("%d", result[0][0]);
for(int i = 1; i < n; i++){
if(i % 2 == 1)
for(int j = 0; j < result[i].size(); j++)
printf(" %d", result[i][j]);
if(i % 2 == 0)
for(int j = result[i].size() - 1; j >= 0; j--)
printf(" %d", result[i][j]);
}
return 0;
}