Uva - 122 - Trees on the level

来源:互联网 发布:北京淘宝摄影基地 编辑:程序博客网 时间:2024/05/21 07:59



难点在于二叉树的生成和宽度优先遍历BFS,最后要注意防止内存泄漏,虽然题目没有内存限制,但是优化考虑还是做一下释放内存,因为这个不是效率型的问题,测试时间短到没有无法显示。

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cctype>#include <cstring>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <algorithm>#include <stack>#include <queue>#include <bitset> #include <cassert> using namespace std;const int maxn = 266;struct Node{bool haveValue; // 是否被赋值过int v; // 结点的值Node* left, *right; // 左右子结点Node() :haveValue(false), left(NULL), right(NULL) {} };Node *root; // 根结点Node* newnode() {return new Node(); // 产生一个新结点}bool failed;void addnode(int v, char* s){int n = strlen(s);Node* u = root; // 从根结点开始往下走for (int i = 0; i < n; i++) {if (s[i] == 'L') {if (u->left == NULL) {u->left = newnode(); // 左结点不存在,就生成一个新的}u = u->left; // 往左走}else if (s[i] == 'R') {if (u->right == NULL) {u->right = newnode(); // 右结点不存在,就生成一个新的}u = u->right; // 往右走} // 右括号直接忽略}if (u->haveValue) {failed = true; // 已经有值了,输入错误,标记起来}u->v = v;u->haveValue = true;}// 防止内存泄漏,新的测试之前记得释放掉之前的树// 这里递归的释放内存void removeTree(Node* u){if (u == NULL) {return;}removeTree(u->left);removeTree(u->right);delete u;}char s[maxn];bool readInput(){failed = false;removeTree(root);root = newnode();while (1) {if (scanf("%s", s) != 1) { // 输入结束return false;}if (!strcmp(s, "()")) {break; // 读取到结束标志}int v;sscanf(&s[1], "%d", &v);addnode(v, strchr(s, ',') + 1);}return true;}// 宽度优先遍历bool bfs(vector<int>& ans){queue<Node*> q;ans.clear();q.push(root);while (!q.empty()) {Node* u = q.front();q.pop();if (!u->haveValue) { // 有结点没有被赋值,输入有错return false;}ans.push_back(u->v);if (u->left != NULL) { // 左结点非空,入队q.push(u->left);}if (u->right != NULL) { // 右结点非空,入队q.push(u->right);}}return true;}int main(){ios::sync_with_stdio(false);vector<int> ans;while (readInput()) {if (!bfs(ans)) {failed = 1;}if (failed) {cout << "not complete\n";}else {for (int i = 0; i < ans.size(); i++) {if (i != 0) {cout << " ";}cout << ans[i];}cout << endl;}}return 0;}




0 0
原创粉丝点击