POJ 2255 Tree Recovery 二叉树恢复
一道和Leetcode的一道题目基本上一样的题目。
给出前序遍历和中序遍历序列,要求依据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出。
Leetcode上有给出后序和中序,恢复二叉树的。
只是事实上算法都是一样的。仿佛又回到了做Leetcode题的那段岁月中了。
还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的能够学习下。
#include <stdio.h>
#include <string>
#include <algorithm>
using std::string; const int MAX_B = 1024;
char buf[MAX_B];
int id = 0, len = 0; inline char getFromBuf()
{
if (id >= len)
{
len = fread(buf, 1, MAX_B, stdin);
id = 0;
}
return buf[id++];
} void getStrFromBuf(string &n)
{
char a = getFromBuf();
while ((a == ' ' || a == '\n') && len) a = getFromBuf(); n.clear();
while ((a != ' ' && a != '\n') && len)//老是写&&,错成||
{
n.push_back(a);
a = getFromBuf();
}
} struct Node
{
char alpha;
Node *left, *right;
explicit Node (char a = ' ') : alpha(a), left(NULL), right(NULL) {}
}; Node *recover(string &preo, int p1, int p2, string &ino, int i1, int i2)
{
if (p1 > p2) return NULL;
Node *root = new Node(preo[p1]); int off = 0;
for ( ; ino[i1+off] != preo[p1]; off++); root->left = recover(preo, p1+1, p1+off, ino, i1, i1+off-1);
root->right = recover(preo, p1+off+1, p2, ino, i1+off+1, i2);
return root;
} void releaAndPrintTree(Node *r)
{
if (r)
{
releaAndPrintTree(r->left);
releaAndPrintTree(r->right);
putchar(r->alpha);
delete r; r = NULL;
}
} int main()
{
string preo, ino;
while (true)
{
getStrFromBuf(preo);
if (len == 0) break;
getStrFromBuf(ino);
releaAndPrintTree(recover(preo, 0, (int)preo.size()-1, ino, 0, (int)ino.size()-1));
putchar('\n');
}
return 0;
}
POJ 2255 Tree Recovery 二叉树恢复的更多相关文章
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
- POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
1.前序遍历的规则:(根左右) (1)访问根节点 (2)前序遍历左子树 (3)前序遍历右子树 对于图中二叉树,前序遍历结果:ABDECF 2.中序遍历的规则:(左根右) (1)中序遍历左子树 (2)访 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- poj 2255 Tree Recovery 分治
Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...
- poj 2255 Tree Recovery(求后序遍历,二叉树)
版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- POJ 2255 Tree Recoveryw(二叉树)
题目原网址:http://poj.org/problem?id=2255 题目中文翻译: Description 小瓦伦丁非常喜欢玩二叉树. 她最喜欢的游戏是用大写字母构造的随机二叉树. 这是她的一个 ...
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
随机推荐
- rabbitmq技术的一些感悟(一)
Rabbitmq 初识rabbitmq RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现.假设不熟悉AMQP,直接看Rabbi ...
- 基于Qt有限状态机的一种实现方式和完善的人工智能方法
基于Qt有限状态机的一种实现方式和完善的人工智能方法 人工智能在今年是一个非常火的方向,当然了.不不过今年,它一直火了非常多年,有关人工智能的一些算法层出不穷.人工智能在非常多领域都有应用,就拿我熟悉 ...
- SQL Server 2008 R2 性能计数器详细列表(三)
原文:SQL Server 2008 R2 性能计数器详细列表(三) SQL Server,Deprecated Features 对象: 监视指定为不推荐使用的功能: SQL Server Depr ...
- 基于VMware的虚拟Linux集群搭建-lvs+keepalived
基于VMware的虚拟Linux集群搭建-lvs+keepalived 本文通过keepalived实现lvsserver的的双机热备和真实server之间的负载均衡.这方面的blog挺多,可是每一个 ...
- 模式识别 - libsvm该函数的调用方法 详细说明
libsvm该函数的调用方法 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26261173 须要载入(load)SVM ...
- Net 项目代码风格
.Net 项目代码风格要求 .Net 项目代码风格要求 PDF版下载:项目代码风格要求V1.0.pdf 代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<.Net 项目代码风格要求&g ...
- Session中StateServer的使用方法
最近项目中用到 Session的StateServer模式,我们知道sessionState有四种模式:off,inProc,StateServer,SqlServer. 而StateServer 是 ...
- JavaScript-2.2 document.write 输出到页面的内容
<html> <head> <meta http-equiv="content-type" content="text/html;chars ...
- oracle HA 高可用性具体解释(之中的一个)
oracle HA 高可用性具体解释(之二,深入解析TAF,以及HA框架) :http://blog.csdn.net/panfelix/article/details/38436197 一.HA F ...
- DevExpress Report 其他常用设计技巧
原文:DevExpress Report 其他常用设计技巧 1 设置默认的打印纸张及页边距 选择Report-打开属性窗口,设置默认边距(Margins)和默认纸张(PaperKind). 2 修改R ...