PAT A1102. Invert a Binary Tree (25) [树的遍历]

本文介绍了一种算法,用于反转二叉树并实现层序和中序遍历。通过存储节点信息并使用递归方法,文章展示了如何找到根节点,并通过排序输出层序遍历结果。

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

Now it’s your turn to prove that YOU CAN invert a binary tree!
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 –
– –
0 –
2 7
– –
– –
5 –

Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

题⽬⼤意:反转⼀棵⼆叉树,给出原⼆叉树的每个结点的左右孩⼦,输出它的层序和前序遍历~
分析: 1. 反转⼆叉树就是存储的时候所有左右结点都交换。
2. ⼆叉树使⽤{id, l, r, index, level}存储每个结点的id, 左右结点,下标值,和当前层数~
3. 根结点是所有左右结点中没有出现的那个结点~
4. 已知根结点,⽤递归的⽅法可以把中序遍历的结果push_back到数组v1⾥⾯,直接输出就是中
序,排序输出就是层序(排序⽅式,层数⼩的排前⾯,相同层数时, index⼤的排前⾯)

#include <vector>
#include <algorithm>
using namespace std;
struct node {
	int id, l, r, index, level;
} a[100];
vector<node> v1;
void dfs(int root, int index, int level) {
	if (a[root].r != -1) dfs(a[root].r, index * 2 + 2, level + 1);
	v1.push_back({root, 0, 0, index, level});
	if (a[root].l != -1) dfs(a[root].l, index * 2 + 1, level + 1);
}
bool cmp(node a, node b) {
	if (a.level != b.level) return a.level < b.level;
	return a.index > b.index;
}
int main() {
	int n, have[100] = {0}, root = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		a[i].id = i;
		string l, r;
		cin >> l >> r;
		if (l != "-") {
			a[i].l = stoi(l);
			have[stoi(l)] = 1;
		} else {
			a[i].l = -1;
		}
		if (r != "-") {
			a[i].r = stoi(r);
			have[stoi(r)] = 1;
		} else {
			a[i].r = -1;
		}
	}
	while (have[root] == 1) root++;
	dfs(root, 0, 0);
	vector<node> v2(v1);
	sort(v2.begin(), v2.end(), cmp);
	for (int i = 0; i < v2.size(); i++) {
		if (i != 0) cout << " ";
		cout << v2[i].id;
	}
	cout << endl;
	for (int i = 0; i < v1.size(); i++) {
		if (i != 0) cout << " ";
		cout << v1[i].id;
	}
	return 0;
}
本项目构建于RASA开源架构之上,旨在实现一个具备多模态交互能力的智能对话系统。该系统的核心模块涵盖自然语言理解、语音转文本处理以及动态对话流程控制三个主要方面。 在自然语言理解层面,研究重点集中于增强连续对话中的用户目标判定效能,并运用深度神经网络技术提升关键信息提取的精确度。目标判定旨在解析用户话语背后的真实需求,从而生成恰当的反馈;信息提取则专注于从语音输入中析出具有特定意义的要素,例如个体名称、空间位置或时间节点等具体参数。深度神经网络的应用显著优化了这些功能的实现效果,相比经典算法,其能够解析更为复杂的语言结构,展现出更优的识别精度与更强的适应性。通过分层特征学习机制,这类模型可深入捕捉语言数据中隐含的语义关联。 语音转文本处理模块承担将音频信号转化为结构化文本的关键任务。该技术的持续演进大幅提高了人机语音交互的自然度与流畅性,使语音界面日益成为高效便捷的沟通渠道。 动态对话流程控制系统负责维持交互过程的连贯性与逻辑性,包括话轮转换、上下文关联维护以及基于情境的决策生成。该系统需具备处理各类非常规输入的能力,例如用户使用非规范表达或对系统指引产生歧义的情况。 本系统适用于多种实际应用场景,如客户服务支持、个性化事务协助及智能教学辅导等。通过准确识别用户需求并提供对应信息或操作响应,系统能够创造连贯顺畅的交互体验。借助深度学习的自适应特性,系统还可持续优化语言模式理解能力,逐步完善对新兴表达方式与用户偏好的适应机制。 在技术实施方面,RASA框架为系统开发提供了基础支撑。该框架专为构建对话式人工智能应用而设计,支持多语言环境并拥有活跃的技术社区。利用其内置工具集,开发者可高效实现复杂的对话逻辑设计与部署流程。 配套资料可能包含补充学习文档、实例分析报告或实践指导手册,有助于使用者深入掌握系统原理与应用方法。技术文档则详细说明了系统的安装步骤、参数配置及操作流程,确保用户能够顺利完成系统集成工作。项目主体代码及说明文件均存放于指定目录中,构成完整的解决方案体系。 总体而言,本项目整合了自然语言理解、语音信号处理与深度学习技术,致力于打造能够进行复杂对话管理、精准需求解析与高效信息提取的智能语音交互平台。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值