题目链接: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(二叉搜索树)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. [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 ...

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  7. 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 ...

  8. [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 ...

  9. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

随机推荐

  1. 生产环境提升rman备份速度----启动块跟踪

    生产环境提升rman备份速度----启动块跟踪 [环境] AIX(5300-08).oracle10g(10.2.0.1.0-64bit) [目标] 因为生产环境数据量较大,欲加快rman备份的速度 ...

  2. if...else if...else

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog ...

  3. 关于js封装框架类库之DOM操作模块(二)

    上一篇基本实现了框架结构,但是与真正能用上的项目框架比较还是存在很多不足,在这又做了些加强与优化 (function ( window, undefined ) { var arr = [], pus ...

  4. hdu 2519 新生晚会 排列组合

    通过阶段性计算减少一次性的大值计算 #include <stdio.h> int main() { int t, a, b, i; __int64 c; scanf("%d&qu ...

  5. 快速检查SQL两表数据是否一致

    1前话 项目内实现了一新功能:克隆数据库. 2目标 克隆并非用SQLSERVER克隆,故完毕后需要检查各表内一些数据与原表一致性.一些表中的某一些列容许不一致. 3实现 将两表的需要检查的几列取出,相 ...

  6. ASP.NET关于Eval的值

    ASP.NET邦定数据“<%#Eval("Sex")%>”运用三元运算符: <%#(Eval(") ? "男" : "女& ...

  7. Median of Sorted Arrays

    (http://leetcode.com/2011/03/median-of-two-sorted-arrays.html) There are two sorted arrays A and B o ...

  8. comparable与comparator比较

    两种比较接口分析 前者应该比较固定,和一个具体类相绑定,而后者比较灵活,它可以被用于各个需要比较功能的类使用. 一个类实现了 Camparable 接口表明这个类的对象之间是可以相互比较的.如果用数学 ...

  9. js基础 1.简单js 语法 关键字 保留字 变量

    简单js JavaScript 是一个松散性的语言 对象属性却不想c中的结构体或者c++ 和java的对象, 对象继承机制 使用原型的prototype(原型链),js的分为三部分ECMAScript ...

  10. Git远程仓库的使用(三)

    1)git remote add : 添加远程仓库 git remote add origin git@github.com:用户名.仓库名.git 2) git push –u origin mas ...