UVa 536 Tree Recovery
来源:互联网 发布:ubuntu内核 编辑:程序博客网 时间:2024/06/05 08:59
思路分析:
简单的根据先序和中序序列重建二叉树,再后序遍历输出即可。
题解:
#include <cstdio>#include <iostream>#include <string>using namespace std;struct Node{char data;Node *left, *right;};string str, pre, in;Node* create(int pl, int pr, int inl, int inr){if(pl > pr) return NULL;Node* root = new Node;root->data = pre[pl];int k;for(int i = inl; i <= inr; i++){if(in[i] == pre[pl]){k = i;break;}}int leftlen = k - inl;root->left = create(pl+1, pl+leftlen, inl, k-1);root->right = create(pl+leftlen+1, pr, k+1, inr);return root;}void postorder(Node* root){if(root != NULL){postorder(root->left);postorder(root->right);printf("%c", root->data);}}int main(){while(getline(cin, str) && str != ""){int k = -1;for(int i = 0; i < str.length(); i++){if(str[i] == ' '){k = i;break;}}pre = str.substr(0, k);in = str.substr(k+1, str.length());int n = pre.length();Node* root = create(0, n-1, 0, n-1);postorder(root);printf("\n");}return 0;}
阅读全文
0 0
- UVA 536 - Tree Recovery
- uva 536Tree Recovery
- uva 536 - Tree Recovery
- UVA 536 Tree Recovery
- UVA 536 Tree Recovery
- UVa, 536 Tree Recovery
- uva 536 - Tree Recovery
- UVa 536 - Tree Recovery
- UVA - 536 Tree Recovery
- UVa 536 Tree Recovery
- UVa 536 - Tree Recovery
- UVA - 536 Tree Recovery
- Uva - 536 - Tree Recovery
- Tree Recovery-UVA 536
- uva 536 - Tree Recovery
- Uva-536 Tree Recovery
- UVA - 536 Tree Recovery
- UVA 536 Tree Recovery
- CSS3实现文字3D效果
- Unity中Input 方法总结
- Python3 [爬虫实战] Redis+Flask 动态维护cookies池(上)
- LeetCode Problem 424: Longest-Repeating-Character-Replacement
- Java IO
- UVa 536 Tree Recovery
- BZOJ 4791: [CERC2016]Free Figurines 模拟
- 职场10大潜规则
- 如何解决跨域请求的问题
- ADB 过滤activity 关键字
- CODEVS--1201 最小数和最大数
- 浅谈模板类vector和array
- c++ 遍历文件夹函数
- <% %>,<%= %>与<%! %>的用途