题目:1385 由前序和中序构建二叉树
2015-08-28 19:08
197 查看
http://ac.jobdu.com/problem.php?pid=1385
蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧。
类似的是有中序和后续构建二叉树。比如http://www.cnblogs.com/kaituorensheng/p/3788533.html
思路很简单 递归构造:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int SIZE = 1000+10;
struct Node{
int v;
Node *left;
Node *right;
Node(){
v = -1;
left = right = NULL;
}
Node(int val, Node *l, Node *r){
v = val;
left = l;
right = r;
}
};
int n;
int head[SIZE],mid[SIZE];
int flag,getNode;
Node* buildTree(int headptr, int midleft, int midright){
/*cout << "*********" << endl;
cout << "head=" << head[headptr] << endl;
cout << "midleft=" << midleft << endl;
cout << "midright=" << midright << endl;*/
Node *father = new Node();
int pos=-1;
if(midleft == midright){
if(head[headptr] == mid[midleft]){
father->left = father->right = NULL;
father->v = head[headptr];
getNode++;
return father;
}else{
return NULL;
}
}
if(midleft > midright)return NULL;
for(int i=midleft;i<=midright;i++){
if(head[headptr] == mid[i])
pos = i;
}
if(pos == -1)return NULL;
int hasleft=0;//,hasright=0;
if(pos == midleft){//意味着左子树为空
father->left = NULL;
}else{
hasleft = 1;
father->left = buildTree( headptr+1 , midleft, pos-1);
if(father->left == NULL)return NULL;
}
if(pos == midright){
father->right = NULL;
}else{
if(hasleft == 1){
//左子树结点个数 pos-midleft
father->right = buildTree(headptr+pos-midleft+1, pos+1, midright);
}else{
father->right = buildTree(headptr+1, pos+1, midright);
}
if(father->right == NULL)return NULL;
}
father->v = head[headptr];
getNode++;
return father;
}
/*void print(Node *nod){
if(flag)putchar(' ');
else flag=1;
printf("%d", nod->v);
}*/
void print(Node *nod){
//if(flag)putchar(' ');
//else flag=1;
printf("%d ", nod->v);
}
void sear(Node *father){
//if(father->left == NULL && father->right == NULL){//叶子
//print(father);
//}
if(father->left)sear(father->left);
if(father->right)sear(father->right);
print(father);
}
int main(){
//freopen("06.txt", "r", stdin);
int in;
while(~scanf("%d", &n)){
flag = getNode = 0;
for(int i=0;i<n;i++){
scanf("%d", &in);
head[i] = in;
}
for(int i=0;i<n;i++){
scanf("%d", &in);
mid[i] = in;
}
Node *ancestor = buildTree(0, 0, n-1);
if(ancestor == NULL || getNode != n){
//if(ancestor == NULL ){
printf("No\n");
}else{
sear(ancestor);
printf("\n");
}
//cout << getNode << endl;
}
return 0;
}
蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧。
类似的是有中序和后续构建二叉树。比如http://www.cnblogs.com/kaituorensheng/p/3788533.html
思路很简单 递归构造:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int SIZE = 1000+10;
struct Node{
int v;
Node *left;
Node *right;
Node(){
v = -1;
left = right = NULL;
}
Node(int val, Node *l, Node *r){
v = val;
left = l;
right = r;
}
};
int n;
int head[SIZE],mid[SIZE];
int flag,getNode;
Node* buildTree(int headptr, int midleft, int midright){
/*cout << "*********" << endl;
cout << "head=" << head[headptr] << endl;
cout << "midleft=" << midleft << endl;
cout << "midright=" << midright << endl;*/
Node *father = new Node();
int pos=-1;
if(midleft == midright){
if(head[headptr] == mid[midleft]){
father->left = father->right = NULL;
father->v = head[headptr];
getNode++;
return father;
}else{
return NULL;
}
}
if(midleft > midright)return NULL;
for(int i=midleft;i<=midright;i++){
if(head[headptr] == mid[i])
pos = i;
}
if(pos == -1)return NULL;
int hasleft=0;//,hasright=0;
if(pos == midleft){//意味着左子树为空
father->left = NULL;
}else{
hasleft = 1;
father->left = buildTree( headptr+1 , midleft, pos-1);
if(father->left == NULL)return NULL;
}
if(pos == midright){
father->right = NULL;
}else{
if(hasleft == 1){
//左子树结点个数 pos-midleft
father->right = buildTree(headptr+pos-midleft+1, pos+1, midright);
}else{
father->right = buildTree(headptr+1, pos+1, midright);
}
if(father->right == NULL)return NULL;
}
father->v = head[headptr];
getNode++;
return father;
}
/*void print(Node *nod){
if(flag)putchar(' ');
else flag=1;
printf("%d", nod->v);
}*/
void print(Node *nod){
//if(flag)putchar(' ');
//else flag=1;
printf("%d ", nod->v);
}
void sear(Node *father){
//if(father->left == NULL && father->right == NULL){//叶子
//print(father);
//}
if(father->left)sear(father->left);
if(father->right)sear(father->right);
print(father);
}
int main(){
//freopen("06.txt", "r", stdin);
int in;
while(~scanf("%d", &n)){
flag = getNode = 0;
for(int i=0;i<n;i++){
scanf("%d", &in);
head[i] = in;
}
for(int i=0;i<n;i++){
scanf("%d", &in);
mid[i] = in;
}
Node *ancestor = buildTree(0, 0, n-1);
if(ancestor == NULL || getNode != n){
//if(ancestor == NULL ){
printf("No\n");
}else{
sear(ancestor);
printf("\n");
}
//cout << getNode << endl;
}
return 0;
}
相关文章推荐
- noip2002 字串变换 (双向宽搜,交替扩展)
- n皇后问题
- MySql主从复制配置示例
- servlet使用
- Attribute is missing the Android namespace prefix——android开发之xml布局文件
- Android应用程序用户界面(三)
- 使用 dd 命令进行硬盘 I/O 性能检测
- vim 配置替换错误E488: Trailing characters
- mybatis 数据库语句 标签
- C语言中的内部名和外部名
- 8皇后位操作解法
- SQL 的执行顺序分析
- JavaScript 运行机制与单线程
- mysql中varchar(N)中的N代表什么
- UITableView
- Codeforces Round #201 (Div. 2) 347A Difference Row(脑洞)
- UIControl
- JME3资源管理之四:心得和小节
- Scala学习第十一天 Scala中的apply实战详解
- Android Studio 获取证书指纹SHA1