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 ...
随机推荐
- 玩转Web之Jsp(一)-----jsp中的静态包含(<%@include file="url"%>)与动态包含(<jsp:include>)
在jsp中include有两种形式,其中<%@include file="url"%>是指令元素,<jsp:include page="" f ...
- redis基础的字符串类型
redis —— 第二篇 基础的字符串类型 我们都知道redis是采用C语言开发,那么在C语言中表示string都是采用char[]数组的,然后你可能会想,那还不简单,当我执行如下命令,肯定是直 接塞 ...
- 第一篇——第一文 SQL Server 备份基础
原文:第一篇--第一文 SQL Server 备份基础 当看这篇文章之前,请先给你的所有重要的库做一次完整数据库备份.下面正式开始备份还原的旅程. 原文出处: http://blog.csdn.net ...
- 第七章——DMVs和DMFs(2)——用DMV和DMF监控索引性能
原文:第七章--DMVs和DMFs(2)--用DMV和DMF监控索引性能 本文继续介绍使用DMO来监控,这次讲述的是监控索引性能.索引是提高查询性能的关键性手段.即使你的表上有合适的索引,你也要时时刻 ...
- 使用API创建AR 贷项通知单
DECLARE --8000000063 l_dummy varchar2(240); l_customer_trx_id ra_customer_trx.customer_trx_id%type; ...
- 如何识别SQL Server中的CPU瓶颈
原文:如何识别SQL Server中的CPU瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2316/how-to-identify-sql-server ...
- nginx conf by linux kernel
#nginx conf by linux kernel net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.ip_local_port_range = 1024 6 ...
- 最具体的历史centos下一个 postfix + extmail + dovecot + maildrop 安装注意事项2014更新
这篇文章原本是2008年发表的,近期几天照此笔记又安装了一遍系统.这里更新一下记录. 作者:wangdy 本文发表于 http://blog.csdn.net/aryang/article/deta ...
- POSIX 螺纹具体解释(1-概要)
线程是有趣的 线程类似于进程.如同进程,线程由内核按时间分片进行管理.在单处理器系统中,内核使用时间分片来模拟线程的并发运行.这样的方式和进程的同样. 而在多处理器系统中,如同多个进程.线程实际上一样 ...
- 结构的具体说明sublime text 2/3的Golang开发环境
1.下载并安装sublime text 2/3 官网下载:http://www.sublimetext.com/ 2.安装成功sublime text后.启动sublime text.选择菜单栏&qu ...