1127 ZigZagging on a Tree 有两个测试点没过,有没有大佬帮忙看下
作者:互联网
- ZigZagging on a Tree (30)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in “zigzagging order” – that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.
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 inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. 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:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
Sample Output:
1 11 5 8 17 12 20 15
————————————————
思路: 1.通过后序和中序建树
2.层序遍历并记录每层节点数
3.根据层数是奇数或偶数,正向或者反向输出每一层的节点
只过了0和2号测试点,想不出哪里错了,有没有大佬帮忙看下,谢谢了
//思路:1.通过后序和中序建树
// 2.层序遍历并记录每层节点数
// 3.根据层数是奇数或偶数,正向或者反向输出每一层的节点
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node{
int data,depth;
node *lchild=NULL,*rchild=NULL;
};
//depnum记录各层的节点数,maxdep记录最深层数
int n,in[30],post[30],depnum[30]={0},maxdep=-1;
//level记录层序遍历结果,print记录最终输出序列
vector<int> level,print;
//后序和中序建树
node *creat(int postl,int postr,int inl,int inr){
if(postl>postr) return NULL;
node *temp=new node;
temp->data=post[postr];
int k;
for(k=inl;k<=inr;k++){
if(in[k]==post[postr]) break;
}
int numleft=k-inl;
temp->lchild=creat(postl,postl+numleft-1,inl,k-1);
temp->rchild=creat(postl+numleft,postr-1,k+1,inr);
return temp;
}
//层序遍历
void levelorder(node *root){
queue<node*> q;
q.push(root);
root->depth=0;
while(!q.empty()){
node *u=q.front();
q.pop();
depnum[u->depth]++;
level.push_back(u->data);
if(u->lchild!=NULL){
u->lchild->depth=u->depth+1;
q.push(u->lchild);
}
if(u->rchild!=NULL){
u->rchild->depth=u->depth+1;
q.push(u->rchild);
}
}
}
int main(){
cin>>n;
for(int i=0;i<n;i++) cin>>in[i];
for(int i=0;i<n;i++) cin>>post[i];
node *root=creat(0,n-1,0,n-1);
levelorder(root);
//实现往返输出
for(int i=0;i<30;i++){
maxdep=max(maxdep,depnum[i]);
}
int index=0;
for(int i=0;i<=maxdep;i++){
if(i%2==0){
for(int j=index+depnum[i]-1;j>=index;j--){
print.push_back(level[j]);
}
}else{
for(int j=index;j<index+depnum[i];j++){
print.push_back(level[j]);
}
}
index+=depnum[i];
}
for(int i=0;i<n;i++){
if(i>0) cout<<' ';
cout<<print[i];
}
return 0;
}
标签:node,测试点,level,int,Tree,1127,depth,print,include 来源: https://blog.csdn.net/weixin_44547757/article/details/100163901