您的位置:首页 > 其它

1020. Tree Traversals (25)和1385,重建二叉树

2015-07-27 09:45 330 查看
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


提交代

——————————

看到寝室的那群禽兽已经快刷完了leetcode,我还在家里水水的。。。。真 是堕落!

这个题目,在云课堂上看到过的,主要是根据后序遍历找到各个子树的根,然后,根据所在的位置,在中序遍历中分出来左右子树。

第一感觉就是要生成树,然后,进行遍历。

主要是对于类似hash 的应用

a.左儿子:通过中序计算出当前节点右子数的节点数量,再通过后序遍历中当前节点位置减去当前节点右子树的数量,后的位置(其实中序就是要来确定当前节点右子树的大小的)。
rsize=right_size(root);
left=hash_post[root]-rsize-1;
b.右儿子就是后序遍历当前node的前一个节点。right=hash_post[root]-1;

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <queue>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define MAX 50

int hash_post[MAX],hash_in[MAX];
int *post,*visited;
int N;

int right_size(int root){
int i=0,size=0;
for(i=hash_in[root]+1; i<=N-1; i++){
if(visited[i]==false) size++;
else break;
}
return size;
}

int main(int argc, char** argv) {
scanf("%d",&N);
post=new int
;
visited=new int
;
for(int i=0; i<N; i++){
scanf("%d",&post[i]);
hash_post[post[i]]=i;
}
for(int i=0; i<N; i++){
int tmp;
scanf("%d",&tmp);
hash_in[tmp]=i;
}
memset(visited,0,N*sizeof(int));
queue<int> q;
q.push(post[N-1]);
int right=N-1;
cout<<post[N-1];
visited[hash_in[post[N-1]]]=true;
while(!q.empty()){
int root=q.front();
int rsize=right_size(root);
int left=hash_post[root]-rsize-1;
if(left>=0 && visited[hash_in[post[left]]]==false){
int left_root=post[left];
cout<<" "<<left_root;
visited[hash_in[left_root]]=true;
q.push(left_root);
}
int right=hash_post[root]-1;
if(right>=0 && visited[hash_in[post[right]]]==false){
int right_root=post[right];
cout<<" "<<right_root;
visited[hash_in[right_root]]=true;
q.push(right_root);
}
q.pop();
}

return 0;
}


——————————————————————————————

看到了重建的一道题目

题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。

输入:输入可能包含多个测试样例,对于每个测试案例,输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数。输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列。输入的第三行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的中序遍历序列。输出:对应每个测试案例,输出一行:如果题目中所给的前序和中序遍历序列能构成一棵二叉树,则输出n个整数,代表二叉树的后序遍历序列,每个元素后面都有空格。如果题目中所给的前序和中序遍历序列不能构成一棵二叉树,则输出”No”。样例输入:81 2 4 7 3 5 6 84 7 2 1 5 3 8 681 2 4 7 3 5 6 84 1 2 7 5 3 8 6
样例输出:
7 4 2 5 8 6 3 1 No

来源:http://ac.jobdu.com/problem.php?pid=1385
这是基本的根据前序、中序构造后序。
#include<iostream>
using namespace std;
typedef struct node
{
int val;
node *left;
node *right;
}node;
bool flag;
node * rebuild(int *pre,int *in ,int length){
node *root=new node();
root->val=pre[0];
root->left=NULL;
root->right=NULL;
int index;
for(index=0;index<length;index++)
if(in[index]==root->val)
break;
if(index>=length){
flag=true;
return NULL;
}
if(index>0)
root->left=rebuild(pre+1,in,index);
if(length-index-1>0)
root->right=rebuild(pre+index+1,in+index+1,length-index-1);
return root;
}
void print_post(node* root){
if(root!=NULL){
print_post(root->left);
print_post(root->right);
cout<<root->val<<" ";
}
}
int main()
{
int n,i;
while( cin>>n){
flag=false;
int *pre=new int
;
int *in=new int
;
for(i=0;i<n;i++)
cin>>pre[i];
for(i=0;i<n;i++)
cin>>in[i];
node *root;
root=rebuild(pre,in,n);
if(flag==true)
cout<<"No"<<endl;
else{
print_post(root);
cout<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: