hdu1710 Binary Tree Traversals

本文介绍了解决UVA548问题的两种方法:使用数组和指针构建二叉树,并给出了从给定的前序和中序遍历序列生成后续遍历序列的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

是 uva548 减弱版,给一棵树的前序,中序输出后续。

两种建树的方法,数组(推荐),指针。

#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <ctime>
#include <assert.h>

#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define eps 1e-8
#define M_PI 3.141592653589793

typedef long long ll;
const ll mod=1000000007;
const int inf=0x7fffffff;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
using namespace std;
const int maxn=1000+10;

int pre_order[maxn],in_order[maxn],l[maxn],r[maxn],ok;

int bulid(int l1,int r1,int l2,int r2)
{
     if(l2>r2) return 0;
     int root=pre_order[l1];
     int p=l2;
     while(in_order[p]!=root) p++;
     int cnt=p-l2;
     l[root]=bulid(l1+1,l1+cnt,l2,p-1);
     r[root]=bulid(l1+cnt+1,r1,p+1,r2);
     return root;
}

void post_order(int root)
{
    if(l[root]!=0)  post_order(l[root]);
    if(r[root]!=0) post_order(r[root]);
    if(!ok) ok=1;
    else printf(" ");
    printf("%d",root);
}

int main()
{
   int n;
   while(~scanf("%d",&n)){
      memset(l,0,sizeof(l));
      memset(r,0,sizeof(r));
      for(int i=1;i<=n;i++) scanf("%d",&pre_order[i]);
      for(int i=1;i<=n;i++) scanf("%d",&in_order[i]);
      bulid(1,n,1,n);
      ok=0;
      post_order(pre_order[1]);
      printf("\n");
   }
}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
   int v;
   Node *left,*right;
};
Node *root;
Node *newnode() {return new Node();}
const int maxn=2000+10;
int pre_order[maxn],in_order[maxn],n,ok;

int Find(int *a,int s,int e,int v)
{
    for(int i=s;i<=e;i++)
      if(a[i]==v) return i;
    return 0;
}
Node* bulid(int l1,int r1,int l2,int r2)
{
   if(l2>r2) return NULL;
   int p=l2;
   while(in_order[p]!=pre_order[l1]) p++;
   int cnt=p-l2;
   Node *t=newnode();
   t->v=pre_order[l1];
   t->right=bulid(l1+cnt+1,r1,p+1,r2);
   t->left=bulid(l1+1,l1+cnt,l2,p-1);
   return t;
}



void post_order(Node *t)
{
   if(t){
     if(t->left) post_order(t->left);
     if(t->right) post_order(t->right);
     if(ok) cout<<" ";
     else ok=1;
     cout<<t->v;
   }
   return;
}

void Delete(Node* t)
{
    if(!t->left) Delete(t->left);
    if(!t->right) Delete(t->right);
    delete t;
}

int main()
{
    while(~scanf("%d",&n)){
        ok=0;
        for(int i=1;i<=n;i++) scanf("%d",&pre_order[i]);
        for(int i=1;i<=n;i++) scanf("%d",&in_order[i]);
        root=bulid(1,n,1,n);
        post_order(root);
        cout<<endl;
        //Delete(root);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值