PAT A1127 ZigZagging on a Tree (30分) (不建树)

PAT甲级:A1127 ZigZagging on a Tree (30分)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in “zigzagging order” – that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1 

Sample Output:

1 11 5 8 17 12 20 15
  • 题意:给一个二叉树的后序和中序遍历序列,要求输出这棵树的 “Z” 字形遍历序列。
  • 分析:这题不需要建树,只用在二叉树中后序序列建树方法的基础上加上depth参数用以记录深度即可,然后通过vector数组依次记录每一层的结点,因为无论前中后遍历二叉树的访问序列都是从上至下从左至右,所以push进vector[层数]同样是按照同一层结点间的顺序的,因此该方式可行,同时在建树方法中记录下出现的最大层数。为了方便记录(其实也差不多),第一层的depth被记为0。
#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int post[N], in[N], maxDepth = -1;
vector<int> ans[N];
void create(int postl, int postr, int inl, int inr, int depth) {
    if (postl > postr) return;
    maxDepth = max(maxDepth, depth);
    ans[depth].push_back(post[postr]);
    int k = inl;
    while (k <= inr && in[k] != post[postr]) k++;
    int numleft = k - inl;
    create(postl, postl + numleft - 1, inl, k - 1, depth + 1);
    create(postl + numleft, postr - 1, k + 1, inr, depth + 1);
}
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &in[i]);
    for (int i = 0; i < n; i++) scanf("%d", &post[i]);
    create(0, n - 1, 0, n - 1, 0);
    printf("%d", ans[0][0]);
    for (int i = 1; i <= maxDepth; i++) {
        if (i % 2 == 0) reverse(ans[i].begin(), ans[i].end());
        for (auto it : ans[i]) printf(" %d", it);
    }
    return 0;
}
### Z字形遍历(Zigzag Traversal)的概念 二叉树的Z字形遍历是一种特殊的层序遍历方式,在这种遍历中,每一层节点按照同的方向访问。奇数层从左到右访问,偶数层则从右到左访问[^1]。 --- ### 实现思路 为了实现Z字形遍历,可以利用两个栈来别存储当前层和下一层的节点。通过交替操作这两个栈,可以在改变原有树结构的情况下完成Z字形遍历。具体方如下: - 使用一个标志变量 `left_to_right` 来控制每层的方向。 - 当前层的节点按指定方向弹出并处理,同时将其子节点按相反顺序压入下一个栈中。 - 完成当前层后切换方向继续处理下一层次。 这种方的时间复杂度为 O(n),其中 n 是树中的节点总数,因为每个节点仅被访问一次。 --- ### Zigzag 遍历的 C 语言实现 以下是基于上述逻辑编写的 C 语言代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 创建新节点函数 TreeNode* createNode(int value) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->val = value; newNode->left = NULL; newNode->right = NULL; return newNode; } void zigzagTraversal(TreeNode* root) { if (!root) return; // 如果根为空,则直接返回 // 初始化两个栈 typedef struct Stack { TreeNode** array; int top; int capacity; } Stack; Stack* stack1 = (Stack*)malloc(sizeof(Stack)); Stack* stack2 = (Stack*)malloc(sizeof(Stack)); stack1->capacity = stack2->capacity = 100; // 假设最大容量为100 stack1->array = (TreeNode**)malloc(stack1->capacity * sizeof(TreeNode*)); stack2->array = (TreeNode**)malloc(stack2->capacity * sizeof(TreeNode*)); stack1->top = stack2->top = -1; // 将根节点推入第一个栈 stack1->array[++stack1->top] = root; int leftToRight = 1; // 方向标记:1 表示从左到右,0 表示从右到左 while (stack1->top != -1 || stack2->top != -1) { if (leftToRight) { // 处理 stack1 中的节点 while (stack1->top != -1) { TreeNode* node = stack1->array[stack1->top--]; printf("%d ", node->val); if (node->left) stack2->array[++stack2->top] = node->left; if (node->right) stack2->array[++stack2->top] = node->right; } } else { // 处理 stack2 中的节点 while (stack2->top != -1) { TreeNode* node = stack2->array[stack2->top--]; printf("%d ", node->val); if (node->right) stack1->array[++stack1->top] = node->right; if (node->left) stack1->array[++stack1->top] = node->left; } } // 切换方向 leftToRight = !leftToRight; } } int main() { // 构建一棵简单的二叉树作为测试数据 TreeNode* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->left = createNode(6); root->right->right = createNode(7); printf("Zigzag Traversal of Binary Tree:\n"); zigzagTraversal(root); // 调用zigzagTraversal函数打印结果 return 0; } ``` --- ### 输出解释 对于上面构建的二叉树,其结构如下所示: ``` 1 / \ 2 3 / \ / \ 4 5 6 7 ``` 执行程序后的输出将是: ``` Zigzag Traversal of Binary Tree: 1 3 2 4 5 6 7 ``` 这表明第一层从左至右读取,第二层从右往左读取,依此类推。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值