pat L2-006. 树的遍历

L2-006. 树的遍历
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2
提交代码

后序遍历从右往从依次是父节点,右子节点,左子节点,于是应该先递归建右子树。

#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
int n, a[35], b[35], k;
struct node {
    int value;
    node *left, *right;
    node() {
        left = right = NULL;
    }
};
void buildTree(int l,int r,node *p) {
    if (l > r) {
        return;
    }
    for (int i = l; i <= r; ++i) {
        if (b[i] == a[k]) {
            k--;
            p->value = b[i];
            p->right = new node();
            p->right->value = -1;
            buildTree(i + 1, r, p->right);
            p->left = new node();
            p->left->value = -1;
            buildTree(l, i - 1, p->left);
            break;
        }
    }
}
void bfs(node *p) {
    queue<node*> q;
    q.push(p);
    while (!q.empty()) {
        node* t = q.front();
        q.pop();
        if (t == p) {
            printf("%d", t->value);
        }
        else {
            if(t->value != -1)
                printf(" %d", t->value);
        }
        if(t->left != NULL)
            q.push(t->left);
        if(t->right != NULL)
            q.push(t->right);
    }
    putchar('\n');
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
    }
    node *head = new node();
    k = n;
    buildTree(1, n, head);
    bfs(head);
    return 0;
}
### 关于 PAT L1-006 题目解析 PAT (Programming Ability Test) 是一项编程能力测试,L1级别的题目旨在考察基本的算法设计能力和编程技巧。对于 L1-006 这道题而言,主要涉及字符串处理以及简单的逻辑判断。 #### 一、题目描述 假设给定一段英文文本,其中可能包含字母、空格以及其他字符。现在需要统计该段文字里各个单词出现次数,并按照字典序输出每个单词及其对应的频次。注意:大小写敏感;非英文字母组成的“词”不计入统计范围之内[^3]。 #### 二、解决方案概述 为了实现上述功能,可以采用如下方法: 1. 使用正则表达式去除所有非字母字符; 2. 将整个字符串转换成小写字母形式(如果不需要区分大小写的场合),这里保持原样因为本题要求大小写敏感; 3. 对清理后的字符串按空白符分割得到一个个独立词语; 4. 构建哈希表记录各单词的数量; 5. 最终遍历哈希表并依据键值顺序打印结果。 下面给出具体的 Java 实现方式: ```java import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner=new Scanner(System.in); Map<String,Integer> wordCountMap=new TreeMap<>(); while(scanner.hasNext()){ String line=scanner.nextLine(); StringBuilder sb=new StringBuilder(); for(char c : line.toCharArray()){ if(Character.isLetter(c)){ sb.append(c); }else{ addWord(sb.toString(),wordCountMap); sb.setLength(0); } } // 处理最后一部分可能是有效单词的情况 addWord(sb.toString(),wordCountMap); } for(Map.Entry<String, Integer> entry:wordCountMap.entrySet()){ System.out.println(entry.getKey()+" "+entry.getValue()); } } private static void addWord(String str,Map<String,Integer> map){ if(str.isEmpty())return; map.put(str,map.getOrDefault(str,0)+1); } } ``` 此程序通过逐行读取输入直到遇到文件结束标志EOF来获取完整的待分析文本。每当遇到非字母字符时就尝试向map中添加之前累积起来的一串连续字母作为新发现的一个单词。最后再单独考虑一次剩余未被加入到任何单词中的结尾片段即可完成全部数据预处理工作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值