F (1169) : A DS二叉树_伪层序遍历构建二叉树


一、题目描述

在这里插入图片描述


二、输入与输出

1.输入

第一行输入t,表示有t个测试样例。
第二行起,每一行首先输入一个正整数n,接下来依次输入n个整数。
以此类推,共输入t个测试样例。

4
5 1 -1 2 3 4
7 1 2 2 3 4 4 3
7 1 2 2 -1 3 -1 3
7 1 2 -1 3 -1 4 -1

2.输出

每三行依次输出每个二叉树的前序、中序、后序遍历。
共输出t个二叉树。
注意输出末尾的空格。

1 2 3 4 
1 3 2 4 
3 4 2 1 

1 2 3 4 2 4 3 
3 2 4 1 4 2 3 
3 4 2 4 3 2 1 

1 2 3 2 3 
2 3 1 2 3 
3 2 3 2 1 

1 2 3 4 
4 3 2 1 
4 3 2 1 

三、参考代码

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
using namespace std;

class TreeNode
{
public:
    TreeNode* lchild;
    TreeNode* rchild;
    TreeNode* parent;
    int data;
    TreeNode(){ lchild = NULL; rchild = NULL; parent = NULL; }
    TreeNode(int num) { lchild = NULL; rchild = NULL; parent = NULL; data = num; }
};

class Tree
{
public:
    TreeNode* root;
    int *arr = new int [len];
    int len;
   

    Tree() {};

    TreeNode* Make()
    {
        TreeNode* r;
        queue<TreeNode*>q;
        r = new TreeNode(arr[0]);
        q.push(r);
        int index = 1;
        while (!q.empty() && index<len)
        {
            TreeNode* cur = q.front();
            q.pop();

            if (index < len && arr[index] != -1)
            {
                cur->lchild = new TreeNode(arr[index]);
                q.push(cur->lchild);
            }
            index++;

            if (index < len && arr[index] != -1)
            {
                cur->rchild = new TreeNode(arr[index]);
                q.push(cur->rchild);
            }
            index++;
        }
        return r;
    }

    void MakeTree(int a[],int l)
    {
        len = l;
        for (int i = 0; i < l; i++)
        {
            arr[i] = a[i];
        }
        root = Make();
    }

    void Prepri(TreeNode* r)
    {
        if (r)
        {
            cout << r->data << " ";
            Prepri(r->lchild);
            Prepri(r->rchild);
        }
    }

    void Inpri(TreeNode* r)
    {
        if (r)
        {
            Inpri(r->lchild);
            cout << r->data << " ";
            Inpri(r->rchild);
        }
    }

    void Pospri(TreeNode* r)
    {
        if (r)
        {
            Pospri(r->lchild);
            Pospri(r->rchild);
            cout << r->data << " ";
        }
    }

    void pri()
    {
        Prepri(root);
        cout << endl;
        Inpri(root);
        cout << endl;
        Pospri(root);
        cout << endl << endl;
    }

};




int main()
{
    int sum;
    cin >> sum;
    while (sum--)
    {
        int len;
        int arr[1000];
        cin >> len;
        for (int i = 0; i < len; i++)
        {
            cin >> arr[i];
        }
        Tree t;
        t.MakeTree(arr, len);
        t.pri();
    }
   
    return 0;
}




  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z1Jxxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值