HDU 1710 Binary Tree Traversals

Problem Description
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
 

Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
 

Output
For each test case print a single line specifying the corresponding postorder sequence.
 

Sample Input
9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
 

Sample Output
7 4 2 8 9 5 6 3 1
题目大意:
给出二叉树的前序,中序,求出二叉树的后序
首先要明白是什么二叉树,以及二叉树的前序,中序,后序分别是什么。
这里有两个写的比较详细的博客,可以作为了解:
关于二叉树:
http://blog.youkuaiyun.com/luckyxiaoqiang/article/details/7518888
关于前序中序得到后序
http://www.cnblogs.com/rain-lei/p/3576796.html
思路很容易想到,利用递归的思想,当遍历完左子树和右子树之后才输出根节点

代码如下:

#include<stdio.h>
int pre[1001],in[1001];
int flag;
//在给定的区间内寻找子树的根节点
int search(int value,int head,int tail)
{
    int i;
    for(i=head;i<=tail;i++)
    {
        if(in[i]==value)
        {
            return i;
        }
    }
    return -1; //在指定区间没找到返回-1
}

int branch(int head,int tail)
{
    int loca,value;
    loca=search(pre[flag],head,tail);
    value=pre[flag];
    if(loca==-1) //若返回-1说明子树为空
        return 0;
    flag++;
    branch(head,loca-1);//先遍历左子树
    branch(loca+1,tail);//再遍历右子树
    //最后输出根节点
    if(value!=pre[0])//后序遍历,根节点一定是在最后一个
        printf("%d ",value);
    else
        printf("%d",value);
    return 0;
}
int main()
{
    int n,i;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
            scanf("%d",&pre[i]);
        for(i=0;i<n;i++)
            scanf("%d",&in[i]);
        flag=0;
        branch(0,n-1);
        printf("\n");
    }
}



数据集介绍:垃圾分类检测数据集 一、基础信息 数据集名称:垃圾分类检测数据集 图片数量: 训练集:2,817张图片 验证集:621张图片 测试集:317张图片 总计:3,755张图片 分类类别: - 金属:常见的金属垃圾材料。 - 纸板:纸板类垃圾,如包装盒等。 - 塑料:塑料类垃圾,如瓶子、容器等。 标注格式: YOLO格式,包含边界框和类别标签,适用于目标检测任务。 数据格式:图片来源于实际场景,格式为常见图像格式(如JPEG/PNG)。 二、适用场景 智能垃圾回收系统开发: 数据集支持目标检测任务,帮助构建能够自动识别和分类垃圾材料的AI模型,用于自动化废物分类和回收系统。 环境监测与废物管理: 集成至监控系统或机器人中,实时检测垃圾并分类,提升废物处理效率和环保水平。 学术研究与教育: 支持计算机视觉与环保领域的交叉研究,用于教学、实验和论文发表。 三、数据集优势 类别覆盖全面: 包含三种常见垃圾材料类别,覆盖日常生活中主要的可回收物类型,具有实际应用价值。 标注精准可靠: 采用YOLO标注格式,边界框定位精确,类别标签准确,便于模型直接训练和使用。 数据量适中合理: 训练集、验证集和测试集分布均衡,提供足够样本用于模型学习和评估。 任务适配性强: 标注兼容主流深度学习框架(如YOLO等),可直接用于目标检测任务,支持垃圾检测相关应用。
根据原作 https://pan.quark.cn/s/a4b39357ea24 的源码改编 MySQL数据库在维护数据完整性方面运用了多种策略,其中包括日志机制和两阶段提交等手段。 接下来将深入阐述这些机制。 1. **日志机制** - **Redo Log**:重做日志记录了数据页内数据行的变更,存储的是更新后的数据。 它遵循预先写入日志的策略,确保在数据库非正常重启后可以恢复数据。 Redo log采用循环写入方式,其容量可以通过`innodb_log_file_size`和`innodb_log_files_in_group`参数进行调整。 日志首先被写入内存中的redo log buffer,随后根据`innodb_flush_log_at_trx_commit`参数的设定决定何时写入硬盘: - 当设置为0时,每分钟由后台进程执行一次刷新操作。 - 当设置为1时,每次事务完成时立即持久化到存储设备,提供最高级别的安全性,但可能会对运行效率造成影响。 - 当设置为2时,事务完成仅将日志写入日志文件,不强制执行磁盘刷新,由操作系统决定何时进行刷新,这样可能会在服务器异常时造成数据丢失。 - **Binlog**:二进制日志记录了所有更改数据库状态的操作,不包括查询和展示类操作,主要用于数据恢复、复制和审核。 Binlog存在Statement、Row和Mixed三种格式,其中Row格式因为记录了行级别变化,能够确保数据不丢失,因此被普遍推荐使用。 Binlog的写入过程与redo log相似,先写入binlog cache,然后在事务提交时写入binlog文件。 `sync_binlog`参数控制了binlog何时写入磁盘,其设定方式与`innodb_flush_log_at_t...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值