由顺序方式存储的完全二叉树进行重建

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#define MAX 10000
using namespace std;
int tree[MAX];
void preverse(int x)
{
    cout<<tree[x]<<" ";
    if(tree[2*x+1] != 0)
        preverse(2*x+1);
    if(tree[2*x+2] != 0)
        preverse(2*x+2);
}
void midverse(int x)
{
 
    if(tree[2*x+1] != 0)
        midverse(2*x+1);
    cout<<tree[x]<<" ";
    if(tree[2*x+2] != 0)
        midverse(2*x+2);
}
void deverse(int x)
{
 
    if(tree[2*x+1] != 0)
        deverse(2*x+1);
    if(tree[2*x+2] != 0)
        deverse(2*x+2);
    cout<<tree[x]<<" ";
}
 
int main() {
    int n;
    cin>>n;
    for (int i = 0; i < n; ++i) {
        cin>>tree[i];
    }
    preverse(0);
    cout<<endl;
    midverse(0);
    cout<<endl;
    deverse(0);
    return 0;
}
 
 

 

posted @ 2020-12-14 19:40  ananasaa  阅读(147)  评论(0编辑  收藏  举报