poj2255
来源:互联网 发布:机器人编程工资待遇 编辑:程序博客网 时间:2024/05/22 16:55
通过遍历确定二叉树结构的问题
题意:给出二叉树的前序遍历和中序遍历,输出二叉树的后序遍历。
思路:二叉树前序遍历的首字符是二叉树的根,后序遍历中位于该字符左边的是左子树的中序遍历,右边是右子树的中序遍历。首先找出根节点,然后递归求解左右子树,最后输出根节点字符。
代码:
#include<string>#include<iostream>using namespace std;string a,b;void tree(int preleft,int preright,int midleft,int midright){ int root,leftsize,midsize; for(root=midleft;root<=midright;root++) if(a[preleft]==b[root]) break; leftsize=root-midleft; midsize=midright-root; if(leftsize>0) tree(preleft+1,preleft+leftsize,midleft,root-1); if(midsize>0) tree(preleft+leftsize+1,preright,root+1,midright); cout<<b[root];}int main(){ while(cin>>a>>b) { tree(0,a.size()-1,0,b.size()-1); cout<<endl; } return 0;}
- poj2255
- poj2255
- poj2255
- poj2255
- poj2255
- POJ2255
- POJ2255
- POJ2255
- poj2255
- poj2255
- poj2255
- poj2255
- POJ2255
- POJ2255
- POJ2255
- POJ2255-递归
- POJ2255 解题报告
- poj2255解题报告
- 用photoshop制作iphone圆角图片
- uiautomator测试框架,深入研究之setText(String)
- 1562 Guess the number
- u盘禁止删除和写入,只能读取的问题解决方法。
- JAVA笔记之继承(二)
- poj2255
- 最难就业季求职成本:北漂求职成本1.5万元
- Windbg双机调试
- Windows下安装squid详解
- Python 标准库 cmd模块
- oracle job 设置自动执行,执行错误,如何解决呢?
- 计算机小Tips
- hdu 4631(最近点对,容器)
- oracle 体系结构图