NOIP 2004 普及组 sdnu 1168.FBI树
2015-07-25 12:49
381 查看
原题链接:
http://210.44.14.31/problem/show/1168
考查树的构造和后序遍历。
代码如下:
http://210.44.14.31/problem/show/1168
考查树的构造和后序遍历。
代码如下:
#include<iostream> #include<cmath> #include<string> #include<cstring> using namespace std; typedef struct node { char fbi; struct node * leftchild, *rightchild; node() :leftchild(NULL), rightchild(NULL){} //构造函数的简写 }*NODE; string numstr; void CreatFbiTree(int left,int right,NODE parent) { if (left == right) //递归结束条件 { if (numstr[left] == '0') parent->fbi = 'B'; else parent->fbi = 'I'; return; } bool b = false, i = false; //判断0,1 for (int j = left; j <= right; j++) { if (numstr[j] == '0'&&!b) b = true; else if (numstr[j] == '1'&&!i) i = true; if (i&&b) break; } if (i&&b) parent->fbi = 'F'; else if (!i&&b) parent->fbi = 'B'; else parent->fbi = 'I'; if (left <= (right + left) / 2) { parent->leftchild = new node(); CreatFbiTree(left, (right + left) / 2, parent->leftchild); } if ((right + left) / 2 + 1 <= right) { parent->rightchild = new node(); CreatFbiTree((right + left) / 2 + 1, right, parent->rightchild); } return; } void PostTraverseTree(NODE root) //后序遍历 { if (NULL != root->leftchild) PostTraverseTree(root->leftchild); if (NULL != root->rightchild) PostTraverseTree(root->rightchild); cout << root->fbi; return; } int main() { int n; cin >> n; NODE root=new node(); cin >> numstr; CreatFbiTree(0, numstr.length() - 1, root); PostTraverseTree(root); cout << endl; return 0; }
相关文章推荐
- java中,各种日期的转换及计算
- 缓动函数 速查
- [cmd]如何设置 Windows 默认命令行窗口大小和缓冲区大小
- HDU 5289 2015 Multi-University Training Contest 1 1002 RMQ+二分
- [转]用 Jsp 的 Session 机制编写的购物车程序
- JQuery中如何查找某种类型的所有元素&选择器
- ngx_hash散列表
- hdoj 2674 N!Again 【好题】
- PHP生成图片验证码、点击切换实例
- winform 创建新注册表时出现“不允许所请求的注册表访问权”,解决办法
- 重设“目录服务还原模式”的系统管理员密码
- leetcode[136]:Single Number
- 深入了解View(四)—— LayoutInflater原理及View测量/布局/绘制总结
- 设计模式之-----外观模式
- 网吧技术应用先行者,争做网吧技术白领人!
- ASP.NET 5 Beta5 对TagHelper带来的变化
- java新手笔记18 类比较
- POJ 3984 迷宫问题 BFS
- Ubuntu Linux14 64位下在Android studio下用gradle编译Andrid项目时发生libz.so.1共享库找不到的解决方法。
- hdoj 2147 kiki's game 【博弈】