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 ...
随机推荐
- loadRunner 11.0 安装及破解
http://jingyan.baidu.com/article/20095761b31b58cb0621b463.html 破解时必须是管理员账户登录.
- 不直接用NSLog
公司中不直接使用NSLog,而是利用宏定义自己的打印函数,将该打印函数写在项目的.pch文件中.调试的时候往往用到好多打印,但发布的时候确不需要.(一下是在公司中的一些处理) 自定义NSLog 一,固 ...
- 在InteliJ IDEA中写Dart及配置IDEA - Dart Plugin
此文运用的是优雅的Markdown而书 Dart官方建议使用的编译器是DartEditor,我下载下来看下,和Eclipse的界面很相像.对于Eclipse,我是既爱又恨,爱它的稳定,恨它的功能没有I ...
- 初识python yield
for sel in response.xpath('//ul/li'): item = DmozItem() item['title'] = sel.xpath('a/text()').extrac ...
- C单链表实现
/* * LinkNode.c * * Created on: Jan 14, 2014 * Author: root */ #include <stdlib.h> #include &l ...
- 【转】关于UItableViewCell的accessoryType属性
转载自:http://blog.csdn.net/kmyhy/article/details/6442351 使用的话,例如: cell.accessoryType = UITableViewCell ...
- 滚动条QScroolBar实现滚屏功能(屏幕过大,覆盖wheelEvent来处理滑轮事件)
环境:Qt5 编译器:Qt Creator 需求:如图 显示区域win 600*300 需要显示的Widget控件show 590*550 则有600*250的show界面无法显示 使用滑块控制sho ...
- HDU 2451 Simple Addition Expression
题目大意:有一个关于 简单加法表达式 的定义告诉你,就是 选一个数字i 如果 i+(i+1)+(i+2) 它的和,没有任何一位进位的话,那就是 一个i的简单加法表达式,求小于n的表达式数目. 题 ...
- js如何判断一个对象是不是Array?(转载)
js如何判断一个对象是不是Array? 在开发中,我们经常需要判断某个对象是否为数组类型,在Js中检测对象类型的常见方法都有哪些呢? typeof 操作符 对于Function, String, Nu ...
- js动画学习(二)
四.简单动画之缓冲运动 实现速度的缓冲,即不同位置的速度不同,越靠近目标值速度越小,所以速度值与目标值与当前值之差成正比.这里要注意一个问题就是物体在运动中速度是连续变化的,不是按照整数变化的,当物体 ...