PAT ~ L2-011. 玩转二叉树 (树的遍历互求 + 交换左右节点)

本文介绍了一种利用中序和前序遍历构建二叉树的方法,并实现了通过层序遍历来交换非叶子节点的左右子树。通过具体代码示例展示了如何构造树结构并进行遍历及节点交换。

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

思路:树的遍历互求,中序前序建树。对于交换非叶子节点的左右孩子,可以通过层序遍历的时候让右子树先入队,左子树后入队。也可以通过遍历交换之后,层序遍历即可。

我采取的链表方式建树,也可以用数组模拟。

右子树先入队:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 35;
int n, in_order[MAXN], pre_order[MAXN];
vector<int> ans;
struct Node
{
    int v;
    Node *left, *right;
};
Node *Root;
Node* build(int L1, int R1, int L2, int R2)
{
    if (L1 > R1) return nullptr;
    Node *root = new Node;
    root->v = pre_order[L2];
    int p = L1;
    while (in_order[p] != root->v) p++;
    int cnt = p - L1;
    root->left = build(L1, p - 1, L2 + 1, L2 + cnt);
    root->right = build(p + 1, R1, L2 + cnt + 1, R2);
    return root;
}
void bfs(Node *root)
{
    queue<Node*> Q;
    Q.push(Root);
    while (!Q.empty())
    {
        Node *u = Q.front(); Q.pop();
        ans.push_back(u->v);
        if (u->right != nullptr) Q.push(u->right);
        if (u->left != nullptr) Q.push(u->left);
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &in_order[i]);
    for (int i = 0; i < n; i++) scanf("%d", &pre_order[i]);
    Root = build(0, n - 1, 0, n - 1);
    bfs(Root);
    for (int i = 0; i < n; i++)
    {
        printf("%d", ans[i]);
        if (i != n - 1) printf(" ");
    }
    printf("\n");
    return 0;
}
/*
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
*/

交换:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 35;
int n, in_order[MAXN], pre_order[MAXN];
vector<int> ans;
struct Node
{
    int v;
    Node *left, *right;
};
Node *Root;
Node* build(int L1, int R1, int L2, int R2)
{
    if (L1 > R1) return nullptr;
    Node *root = new Node;
    root->v = pre_order[L2];
    int p = L1;
    while (in_order[p] != root->v) p++;
    int cnt = p - L1;
    root->left = build(L1, p - 1, L2 + 1, L2 + cnt);
    root->right = build(p + 1, R1, L2 + cnt + 1, R2);
    return root;
}
void Reverse(Node *root)
{
    if (root == nullptr) return ;
    Node *t= root->left;
    root->left = root->right;
    root->right = t;
    Reverse(root->left);
    Reverse(root->right);
}
void bfs(Node *root)
{
    queue<Node*> Q;
    Q.push(Root);
    while (!Q.empty())
    {
        Node *u = Q.front(); Q.pop();
        ans.push_back(u->v);
        if (u->left != nullptr) Q.push(u->left);
        if (u->right != nullptr) Q.push(u->right);
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &in_order[i]);
    for (int i = 0; i < n; i++) scanf("%d", &pre_order[i]);
    Root = build(0, n - 1, 0, n - 1);
    Reverse(Root);
    bfs(Root);
    for (int i = 0; i < n; i++)
    {
        printf("%d", ans[i]);
        if (i != n - 1) printf(" ");
    }
    printf("\n");
    return 0;
}
/*
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值