2018-3-6

按照王道机试书上的思路再做了一遍,先根据先序和中序建树,然后后序遍历。

静态分配数组用于建树,可以返回数组地址当作结点指针。

#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[],n; struct Node
{
Node *lson,*rson;
int num;
}node[]; //静态分配一个数组用于建树 int cntn; Node *create(int num)
{
node[cntn].lson=node[cntn].rson=NULL;
node[cntn].num=num;
return &node[cntn++]; //返回数组元素的地址
} Node *build(int ph,int ih,int len)
{
Node *now=create(pre[ph]);
int i;
for(i=;pre[ph]!=in[i];i++);
if(i!=ih)
now->lson=build(ph+,ih,i-ih);
if(i!=ih+len-)
now->rson=build(ph+i-ih+,i+,len-(i-ih)-);
return now;
} int cnt;
void postOrder(Node *rt)
{
if(rt==NULL)
return;
postOrder(rt->lson);
postOrder(rt->rson);
printf("%d",rt->num);
if(++cnt==n)
printf("\n");
else
printf(" ");
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cntn=cnt=;
for(int i=; i<=n; i++)
scanf("%d",&pre[i]);
for(int i=; i<=n; i++)
scanf("%d",&in[i]);
Node *rt=build(,,n);
postOrder(rt);
}
return ;
}

2018-3-3

又做了一遍,这次是自己根据思想自己码了一遍

看了上次的代码,感觉还是上次的好一点

#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[],cnt,n; void getPost(int pl,int pr,int il,int ir)
{
if(pr<pl||ir<il)
return;
int loc;
for(int i=il; i<=ir; i++)
if(in[i]==pre[pl])
{
loc=i;
break;
}
if(<loc-il)
getPost(pl+,pl+loc-il,il,loc-);
if(<ir-loc)
getPost(pr-(ir-loc)+,pr,loc+,ir);
printf("%d",pre[pl]);
cnt++;
if(cnt==n)
printf("\n");
else
printf(" "); }
int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=;
for(int i=; i<=n; i++)
scanf("%d",&pre[i]);
for(int i=; i<=n; i++)
scanf("%d",&in[i]);
getPost(,n,,n);
}
return ;
}

2018-1-3

研究生考试初试结束一个周了,开始准备复试了,又要开始刷题了。

给定一个二叉树前序和中序,确定后序。

#include<iostream>
#include<cstdio>
using namespace std; int pre[],in[]; void getPostOrder(int ph,int ih,int len,int flag)
{
if(len<)
return;
int i;
for(i=; pre[ph]!=in[ih+i]; i++); //在中序中找到根节点
getPostOrder(ph+,ih,i,);     //处理左子树
getPostOrder(ph+i+,ih+i+,len-i-,); //处理右子树
if(flag)
printf("%d\n",pre[ph]);
else
printf("%d ",pre[ph]);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<n; i++)
scanf("%d",&pre[i]);
for(int i=; i<n; i++)
scanf("%d",&in[i]);
//cout<<"*"<<endl;
getPostOrder(,,n,);
}
return ;
}

HDU_1710_二叉树前序中序确定后序的更多相关文章

  1. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  2. 二叉树的前序和中序得到后序 hdu1710

    今天看学长发过来的资料上面提到了中科院机试会有一个二叉树的前序中序得到后序的题目.中科院的代码编写时间为一个小时,于是在七点整的时候我开始拍这个题目.这种类型完全没做过,只有纸质实现过,主体代码半个小 ...

  3. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  4. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  5. c/c++ 用前序和中序,或者中序和后序,创建二叉树

    c/c++ 用前序和中序,或者中序和后序,创建二叉树 用前序和中序创建二叉树 //用没有结束标记的char*, clr为前序,lcr为中序来创建树 //前序的第一个字符一定是root节点,然后去中序字 ...

  6. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  7. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  8. PHP递归方法实现前序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...

  9. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

随机推荐

  1. hdu4908 &amp; BestCoder Round #3 BestCoder Sequence(组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 BestCoder Sequence Time Limit: 2000/1000 MS (Jav ...

  2. js闭包的本质

    js之所以会有闭包,是因为js不同于其他规范的语言,js允许一个函数中再嵌套子函数,正是因为这种允许函数嵌套,导致js出现了所谓闭包. function a(){ function b(){ }; b ...

  3. UESTC93 King's Sanctuary

    King's Sanctuary Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  4. Lightoj 1020 - A Childhood Game

    Allice先拿,最后拿球的输. Bob先拿,最后拿球的赢. 考虑Alice先拿球,当n=1时 Alice输  记dp[1]=0; n=2,  dp[2]=1 n=3,  dp[3]=1 因为n=1, ...

  5. YTU 1004: 1、2、3、4、5...

    1004: 1.2.3.4.5... 时间限制: 1000 Sec  内存限制: 64 MB 提交: 1275  解决: 343 题目描述 浙江工商大学校园里绿树成荫,环境非常舒适,因此也引来一批动物 ...

  6. xcode 8.1 (8B62)真机调试配置

    1.点击菜单栏中的Xcode->Preferences->Accounts,如图: 点击上图左下角中的“+”号,登陆一个Apple id(前提已经有了一个apple id账号), 2.然后 ...

  7. bzoj 2783: [JLOI2012]树【树上差分】

    注意是等于s不是大于s dfs,用set或者map存这条链到root的点权和sum[u],更新答案的时候查一下有没有s-sum[u]即可 #include<iostream> #inclu ...

  8. 最小割树Gomory–Hu tree

    fanhq666地址:http://fanhq666.blog.163.com/blog/static/8194342620113495335724/ wiki地址(证明):https://en.wi ...

  9. bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】

    都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...

  10. 【NOIP2006】作业调度方案 {语文难题}

    Description: 我们现在要利用 m 台机器加工 n 个工件,每个工件都有 m 道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间.  每个工件的每个工序称为一个 ...