POJ 2255 重构二叉树
2013-08-31 17:16
288 查看
#include<iostream> #include<string> #include<fstream> using namespace std; //#define DEBUG static const int MAXNODES = 500; struct node { char c; node *lc; node *rc; }; static struct node nodes[MAXNODES]; static int p = 0; struct node * create(string in, string pre) { struct node *root = NULL; if (pre.length() > 0) { root = &nodes[p++]; root->c = pre[0]; int i = in.find(root->c); root->lc = create(in.substr(0, i), pre.substr(1, i)); root->rc = create(in.substr(i + 1), pre.substr(i+1)); } return root; } void postorder(struct node* root) { if (!root) return; postorder(root->lc); postorder(root->rc); cout << root->c ; } int main() { #ifdef DEBUG fstream cin("G:\\book\\algorithms\\acm\\Debug\\dat.txt"); #endif string pre, in; while (cin >> pre >> in) { postorder(create(in, pre)); cout << "\n"; } return 0; }
采用递归的方法,节点预先分配,可能分配的过多了。
对string 的find()和substr()的掌握。
相关文章推荐
- poj 2255 Tree Recovery(做为二叉树的练笔挺不错的)
- A - Tree Recovery POJ - 2255 (二叉树)
- poj 2255二叉树
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery ( 二叉树)
- POJ 2255 Tree Recovery(二叉树重建)
- poj 2255 二叉树的后序遍历
- POJ 2255(二叉树问题)
- POJ2255,Tree Recovery,二叉树重建
- ACM--二叉树-已知前中序求后序--POJ-2255
- poj 2255 二叉树 已知先序和中序,求后序
- poj 2255 Tree Recovery(二叉树)
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
- poj 2255 Tree Recovery(二叉树的遍历)
- POJ 2255 根据二叉树的前序和中序序列来重建二叉树
- poj 2255 Tree Recovery(求后序遍历,二叉树)
- POJ 2255 Tree Recovery【二叉树重建】
- 通过遍历求解二叉树结构poj2255
- POJ 2255 经典的重建二叉树
- B - Tree Recovery POJ - 2255 (二叉树)