hihoCoder 1049 后序遍历
2015-08-01 10:22
573 查看
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct BTreeNode{ char e; BTreeNode *left; BTreeNode *right; }; typedef BTreeNode* BTree; BTreeNode *createBTreeNode(char e) { BTreeNode *nd = new BTreeNode; nd->e = e; nd->left = NULL; nd->right = NULL; return nd; } int findChar(const char *str, int s1, int s2, char c) { if (!str || s2 < s1 || s1 < 0 || s2 >= strlen(str)) return -1; for (int i = s1; i <= s2; i++) { if (str[i] == c) return i; } return -1; } BTreeNode *getRoot(char *pre, int p1, int p2, char *in, int i1, int i2) { char rootch = pre[p1]; if (!pre || p2 < p1 || p1 < 0 || p2 >= strlen(pre) || !in || i2 < i1 || i1< 0 || i2 >= strlen(in)) return NULL; int tmp = findChar(in, i1, i2, rootch); if (tmp < 0) return NULL; BTreeNode *nd = createBTreeNode(rootch); nd->left = getRoot(pre, p1 + 1, p1 + tmp - i1, in, i1, tmp - 1); nd->right = getRoot(pre, p1 + tmp - i1 + 1, p2, in, tmp + 1, i2); return nd; } BTree createBTree(char *pre, char *in) { if (!pre || !in) return NULL; return getRoot(pre, 0, strlen(pre) - 1, in, 0, strlen(in) - 1); } void post_order(BTree t) { if (!t) return; post_order(t->left); post_order(t->right); printf("%c", t->e); } int main(){ char pre[30]; char in[30]; scanf("%s",pre); scanf("%s",in); BTree t = createBTree(pre, in); post_order(t); printf("\n"); return 0; }
相关文章推荐
- P-Called-Party-ID 头域的应用说明
- 1023. Have Fun with Numbers (20)
- Unity-Attribute
- Github免费账户使用”私有库“
- css 7.30
- 自定义ImageView
- 集合
- 分解质因数
- All in All - poj 1936 (子串)
- STM32中GPIO口的8种模式
- 新的公司,新的技术
- 值语义与对象语义
- CABasicAnimation animationWithKeyPath
- 万丈高楼从0开始。
- 最短路
- 杂项设备的学习
- 安卓设备适配终极杀器-谷歌百分比布局库的使用
- 双系统IOS\windows7 换成Windows10后果
- hdu3047 Zjnu Stadium && HDU 3038 How Many Answers Are Wrong (带权并查集)
- MySQL优化