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 ...
随机推荐
- iOS设计模式——MVC(Model-View-Controller)
Modol View Controller(MVC)是一种最早的也是最成功的可重用的设计模式,70年代的时候首次在smaltalk编写的程序中成功使用.基于MVC设计 模式,Cocoa整体架构可以划分 ...
- js中Date对象
Date常用的几个方法: var oDate=new Date(); oDate.getHours()方法是获取当前的小时 oDate.getMinutes()方法获取当前的分钟 oDate.getS ...
- Foundation NSMutableArray遍历,选取出符合条件的所有对象
一.查找数组中一个元素,找到后立即返回 当遍历数组只需要返回其中一个符合条件的元素时,使用 indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, ...
- ACM比赛
Description A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first bana ...
- Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)
关于依赖注入, 这篇博文写的非常简单易懂. https://github.com/android-cn/blog/tree/master/java/dependency-injection 此外, 博 ...
- 【转】IOS 输入框被键盘遮盖的解决方法
做IOS开发时,难免会遇到输入框被键盘遮掩的问题.上网上搜索了很多相关的解决方案,看了很多,但是由衷的觉得太麻烦了. 有的解决方案是将视图上的所有的东西都添加到一个滚动视图对象( UIScrollVi ...
- IOS FoundationKit (NSString) 知识汇总
1. interface 代表类声明,@implement 代表类实现,@encode 代表将object-c 中类型转换成指定字符串 2 当前char * 类型转化为字符串需要转化为NSStrin ...
- android-JSON解析
构建JSON文本 方法1. // 假设现在要创建这样一个json文本 // { // "phone" : ["12345678", "87654321 ...
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
原文 Json.Net系列教程 3.Json.Net序列化和反序列化设置 上节补充 首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framewo ...
- poj 2153 Rank List(查找,Map)
题目链接:http://poj.org/problem?id=2153 思路分析: 判断Li Ming的成绩排名,需要在所有的数据章查找成绩比其高的人的数目,为查找问题. 查找问题可以使用Hash表, ...