PAT Tree Traversals Again

用了两种方法,一个是复原了二叉树,一个是直接根据前中序求后序。


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <stack>


using namespace std;


typedef struct node{
    int data;
    struct node *left;
    struct node *right;
}Node,*b_tree;

vector<int> v1,v2; // v1 is pre  v2 is in;
vector<int> v3;
b_tree insert(int preL,int preR,int inL,int inR){
    if (inL > inR) {
        return NULL;
    }
    int i;
    for (i=inL; i<inR; i++) {
        if (v2[i] == v1[preL]) {
            break;
        }
    }
    b_tree node = new Node;
    node->data = v1[preL];
    int numleft = i - inL;
    int numright = inR - numleft;
    node->left = insert(preL+1, preR-numright, inL, i-1);
    node->right = insert(preL+numleft+1, preR, i+1, inR);
    return node;
}



void post(b_tree root){
    if(root){
        post(root->left);
        post(root->right);
        //        printf("%c ",root->data);
        v3.push_back(root->data);
        
    }
}


int main(){
    int n;
//        printf("hello\n");
    scanf("%d",&n);
    char str[10];
//    string str;
    stack<int> s;
    for (int i=0; i<2*n; i++) {
//        cin.getline(str, 8);
        scanf("%s",str);
        if (str[1] == 'u') {
            int cc;
            scanf("%d",&cc);
            s.push(cc);
            v1.push_back(cc);
        }else{
            int a = s.top();
            v2.push_back(a);
            s.pop();
        }
    }
    b_tree root = insert(0, n-1, 0, n-1);
    post(root);
    for (int i=0; i<v3.size(); i++) {
        printf("%d",v3[i]);
        if (i != v3.size()-1) {
            printf(" ");
        }
    }
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <stack>
#include <queue>


using namespace std;
int N;
vector<int> pre;
vector<int> in;
//vector<int> post[N];
//int *post = (int*)malloc(sizeof(int)*N);
int post[40];
stack<int> s;

int space = 0;
void post_order(vector<int> pre,vector<int> in){
    int i,left,right;
    for (i=0; i<pre.size(); i++) {
        if (pre[0] == in[i]) {
            left = i;
            right = pre.size()-left-1;
            if (left) {
                vector<int> part1(pre.begin()+1,pre.begin()+left+1);
                vector<int> part2(in.begin(),in.begin()+left);
                post_order(part1, part2);
            }
            if (right) {
                vector<int> part3(pre.end()-right,pre.end());
                vector<int> part4(in.end()-right,in.end());
                post_order(part3, part4);
            }
            printf("%d",pre[0]);
            space++;
            if (space < N) {
                printf(" ");
            }
            break;
        }
    }
}




int main(){
//    printf("hello\n");
    scanf("%d",&N);
//    memset(*post, -1, sizeof(post));
    for (int i=0; i<2*N; i++) {
        char str[10];
        scanf("%s",str);
        if (str[1] == 'u') {
            int cc;
            scanf("%d",&cc);
            s.push(cc);
            pre.push_back(cc);
        }else{
            in.push_back(s.top());
            s.pop();
        }
    }
    
    post_order(pre,in);
//    for (int i=0; i<N; i++) {
//        printf("%d ",post[i]);
//    }
//
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值