Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255
思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树;根据中序遍历(如ABCDEFG),已知根结点(D),
可以知道在根结点左边的为左子树结点(ABC),右边为右子树结点(EFG);可以求出左子树与右子树结点个数;已知道左右子树
结点个数(分别为3个),根据先序遍历(如DBACEGF)可知其左子树的先序遍历(BAC)与右子树的先序遍历(EGF);左右子树
的先序遍历与中序遍历可知,递归构造树再后序遍历求解。
代码如下:
#include <iostream>
#include <string>
using namespace std; struct TreeNode;
typedef TreeNode *SearchTree;
typedef char ElementType;
struct TreeNode
{
ElementType Element;
SearchTree Left;
SearchTree Right;
}; void PostOrder(SearchTree T);
SearchTree Insert(ElementType X, SearchTree T);
SearchTree CreateTree(string PreOrder, string InOrder, SearchTree T); int main()
{
string PreOrder, InOrder; while (cin >> PreOrder >> InOrder)
{
SearchTree T = NULL; T = CreateTree(PreOrder, InOrder, T);
PostOrder(T);
cout << endl;
} return ;
} SearchTree Insert(ElementType X, SearchTree T)
{
if (T == NULL)
{
T = (SearchTree)malloc(sizeof(struct TreeNode));
if (T == NULL)
{
printf("Out of space");
return NULL;
}
T->Element = X;
T->Left = T->Right = NULL;
}
else
if (X < T->Element)
T->Left = Insert(X, T->Left);
else
if (X > T->Element)
T->Right = Insert(X, T->Right); return T;
} void PostOrder(SearchTree T)
{
if (T != NULL)
{
PostOrder(T->Left);
PostOrder(T->Right);
printf("%c", T->Element);
}
} SearchTree CreateTree(string PreOrder, string InOrder, SearchTree T)
{
int Index;
char Root; if (PreOrder.length() > )
{
Root = PreOrder[];
T = Insert(Root, T); Index = InOrder.find(Root);
T->Left = CreateTree(PreOrder.substr(, Index), InOrder.substr(, Index), T->Left);
T->Right = CreateTree(PreOrder.substr(Index + ), InOrder.substr(Index + ), T->Right);
} return T;
}
Poj 2255 Tree Recovery(二叉搜索树)的更多相关文章
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
随机推荐
- tomcat 7 无法打开管理页面
在配置文件tomcat-users.xml中添加如下内容即可. <role rolename="admin"/> <role rolename="man ...
- BootStrap 智能表单系列 七 验证的支持
但凡是涉及到用户编辑信息然后保存的页面,都涉及到一个数据是否符合要求的检查,需要客服端和服务器端的校验的问题: 客服端的校验主要是为了提高用户体验,而服务器端的校验为了数据的合格性 该插件也为您支持到 ...
- 解决No Hibernate Session bound to thread, and configuration does not allow creat。。。
applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Javaweb统计在线人数的小栗子
最近在学习Javaweb相关的内容(不黑不吹之前对web开发零基础),下面通过一个统计在线人数的小栗子讲讲Servlet监听器吧 开发环境 eclipse tomcat 7 先说说这个小栗子的构思: ...
- js正则语法
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数 ...
- Mac OS X 下 TAR.GZ 方式安装 MySQL 5.7+
方法: http://www.widlabs.com/article/mac-os-x-install-mysql-57-with-tar-gz mysql下载地址:http://www.mysq ...
- .net mvc Authorization Filter,Exception Filter与Action Filter
一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...
- MVC-03 控制器(5)
八.动作过滤器 有时在运行Action之前或之后会需要运行一些逻辑运算,以及处理一些运行过程中所生成的异常状况,为了满足这个需求,ASP.NET MVC提供动作过滤器(Action Filter)来处 ...
- linux服务器安全小知识
使用单用户模式进入系统 Linux启动后出现boot:提示时,使用一个特殊的命令,如linuxsingle或linux 1,就能进入单用户模式(Single-User mode).这个命令非常有 ...
- SIM卡厂商的识别方法
ICCID(SIM卡号码)的定义应该是: 1-6位:国际移动运营商识别码(IMSI),898600为中国移动,898601为中国联通 7-20位:移动和联通的定义是不同的. 中国移动: 第7.8 ...