1 条题解

  • 1
    @ 2025-4-27 13:52:51

    #include<bits/stdc++.h>

    using namespace std;

    int arr[12000];

    typedef struct TreeNode

    {

    int data,level;

    TreeNode

    *left,*right,*head;//attention

    TreeNode

    };

    TreeNode *establish(int i,int n)

    {

    TreeNode *head=new TreeNode;

    head->left=NULL;

    head->right=NULL;

    head->data=arr[i];

    if(2i<=n+1&&arr[2i]!=0)

    head->left=establish(2*i,n+1);

    if(2i+1<=n+1&&arr[2i+1]!=0)

    head->right=establish(2*i+1,n+1);

    return head;

    }

    void preorder(TreeNode *head)

    {

    if(head==NULL)

    return;

    cout<<head->data<<" ";

    preorder(head->left);

    preorder(head->right);

    }

    void inorder(TreeNode *head)

    {

    if(head==NULL)

    return;

    inorder(head->left);

    cout<<head->data<<" ";

    inorder(head->right);

    }

    void afterorder(TreeNode *head)

    {

    if(head==NULL)

    return;

    afterorder(head->left);

    afterorder(head->right);

    cout<<head->data<<" ";

    }

    int main()

    {

    for(int i=1;i<=7;i++)

    cin>>arr[i];

    TreeNode *head=establish(1,7);

    preorder(head);

    cout<<endl;

    inorder(head);

    cout<<endl;

    afterorder(head);

    cout<<endl;

    return 0;

    }

    • 1

    信息

    ID
    848
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    29
    已通过
    12
    上传者