nyoj221 已知条件构造二叉树
来源:互联网 发布:淘宝网秋冬婴儿服装 编辑:程序博客网 时间:2024/05/18 00:56
链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=221
由于二叉树本身是递归的结构,前序遍历的第一个节点一定是根节点,而通过对后序的结果进行查找,后续第一个节点的距离与前序第一个相同的结点的距离即为左子树的节点的个数,同理右子树同理、
#include<iostream>
#include<stdio.h>
#include<cstring>
#define Max 27
using namespace std;
char pstr[Max],instr[Max];
struct Node
{
char n;
Node *leftchild,*rightchild;
Node(char s):n(s),leftchild(NULL),rightchild(NULL) {}
};
void postorder(Node *roo);
Node* Rebulid(char pstr[],char instr[],int len);
int main()
{
while(scanf("%s%s",&pstr,&instr)!=EOF)
{
Node *root=Rebulid(pstr,instr,int(strlen(pstr)));
postorder(root);
cout<<endl;
}
system("pause");
return 0;
}
Node* Rebulid(char pstr[],char instr[],int len)
{
if(len<=0) return NULL;
Node *newNode=new Node(pstr[0]);
int k,temp;
for(k=0;k<len;++k)
{
if(instr[k]==pstr[0])
{
temp=k;
break;
}
}
newNode->leftchild=Rebulid(pstr+1,instr,temp);
newNode->rightchild=Rebulid(pstr+temp+1,instr+temp+1,len-temp-1);
return newNode;
}
void postorder(Node *roo)
{
if(roo!=NULL)
{
postorder(roo->leftchild);
postorder(roo->rightchild);
cout<<roo->n;
}
else return;
}
- nyoj221 已知条件构造二叉树
- NYOJ221二叉树重建(已知先序和中序求后序)
- nyoj221 nyoj756 重建二叉树
- NYOJ221 Tree(二叉树遍历)
- 已知二叉树前序中序,构造二叉树
- 已知二叉树后序中序,构造二叉树
- 重建二叉树NYOJ221题 && NYOJ756题
- 二叉树构造 (已知先序 和中序)
- 已知先序+中序构造二叉树,已知后序+中序构造二叉树(C语言)
- 二叉树已知前序中序求后序
- 二叉树已知中序前序求后序
- 二叉树(三)——已知遍历序列构造二叉树(java版)
- 二叉树已知先序中序求后序、已知中序后序求先序
- 二叉树已知先序中序求后序,已知后序中序求先序
- 【工程】二叉树已知前序/中序的顺序,构造树的递归等实现
- 二叉树:已知前序&&中序或中序&&后序构造树
- 已知中序与后序,或者中序与先序,构造二叉树
- 已知中序与后序,或者中序与先序,构造二叉树
- winform 基于log4net的日志框架封装
- 构建树对象封装信息
- J2EE简介
- nyoj113
- SELinux总结
- nyoj221 已知条件构造二叉树
- 怎样使用vm安装fedora
- 迷宫问题 bfs
- C++内存分配
- IOS6新特征
- iOS6新特征:UIRefreshControl[下拉刷新]使用示例
- [游戏技术]求生之路服务器参数配置
- [游戏技术]求生之路服务器指令
- tomcat的安装问题