7-2 根据后序和中序遍历输出前序遍历

练习4.1 根据后序和中序遍历输出先序遍历 (25分) 练习4.1 根据后序和中序遍历输出先序遍历 (25分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。 输入格式: 第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。 输出格式: 在一行中输出Preorder: 以及该树的先序遍历结果。数字间有1个空格,行末不得有多余空格。 输入样例: 7 2 3 1 5 7 6 4 1 2 3 4 5 6 7 输出样例: Preord 阅读详情

7-2 根据后序和中序遍历输出前序遍历

分数 25

全屏浏览

切换布局

作者 DS课程组

单位 浙江大学

本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的前序遍历结果。

输入格式:

第一行给出正整数 n (≤30),是树中结点的个数。随后两行,每行给出 n 个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。

输出格式:

在一行中输出Preorder: 以及该树的前序遍历结果。数字间有1个空格,行末不得有多余空格。

输入样例:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例:

Preorder: 4 1 3 2 6 5 7
#include<bits/stdc++.h>
using namespace std;
int h[31],z[31],n;
void build(int a1,int a2,int b1,int b2)
{
    if(a1>a2) return;
    int rt=h[a2],x=b1;
    while(rt!=z[x]) x++;
    cout<<" "<<rt;
    build(a1,a1+x-b1-1,b1,x-1);
    build(a1+x-b1,a2-1,x+1,b2);
}
int main()
{
 cin>>n;
    for(int i=0;i<n;i++) cin>>h[i];
    for(int i=0;i<n;i++) cin>>z[i];
    cout<<"Preorder:";
    build(0,n-1,0,n-1);
}

4-15 根据后序和中序遍历输出先序遍历 (15 分) 4-15 根据后序和中序遍历输出先序遍历 (15 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。 输入格式: 第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。 输出格式: 在一行中输出Preorder: 以及该树的先序遍历结果。数字间有1个空格,行末不得有多余空格。 输入样例: 7 2 3 1 5 7 6 4 1 2 3 4 5 6 7 结尾无空行 输出样例: 阅读详情

相关推荐

二叉树已知后序和中序遍历求前序遍历,C++代码

二叉树已知后序和中序遍历求前序遍历,C++编写已通过编译

根据后序和中序遍历输出前序遍历(超详细!)

根据后序和中序遍历输出前序遍历分数 25全屏浏览切换布局作者 DS课程组单位 浙江大学本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的前序遍历结果。输入格式:第一行给出正整数 n (≤30),是树中结点的个数。随后两行,每行给出 n 个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。输出格式:在一行中输出Preorder: 以及该树的前序遍历结果。数字间有1个空格,行末不得有多余空格。输入样例:7。

m0_75262437的博客 1010

已知中序遍历和后序遍历求前序遍历

已知中序遍历和后序遍历,求前序遍历。有比较详尽的中文注释。

结构与算法 7-23 还原二叉树 (25 分)(30行精简 队列构造)

7-23还原二叉树(25分) 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。 输入格式: 输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。 输出格式: 输出为一个整数,即该二叉树的高度。 输入样例: 9 ABDFGHIEC FDHGIBEAC 输出样例: ...

ItaLink 609

从ViT到Swin Transformer:一文搞懂Transformer在CV中的进化史(附实战代码)

本文系统梳理了Transformer在计算机视觉领域的演进历程,从开创性的Vision Transformer(ViT)引入全局注意力机制,到DETR实现端到端目标检测,再到里程碑式的Swin Transformer通过滑动窗口和层次化设计高效桥接CNN与ViT。文章深入剖析了各模型的核心思想、技术突破与实战应用,并提供了可运行的代码示例,帮助读者从理论到实践全面掌握这一关键技术脉络。

cola5的博客 713

Cesium中实现GPU计算的泥石流地形侵蚀

通过模拟分析,可以提前识别可能发生泥石流的区域,为防灾减灾提供依据,及时发布预警,避免或减少人员伤亡和财产损失。:在建设公路、桥梁、水库等基础设施时,通过泥石流模拟分析,可以预测泥石流可能对这些设施的影响,帮助工程设计者采取合适的防护措施,确保设施的安全性。:模拟分析有助于为灾后救援工作提供科学依据,确定受灾区域的范围、救援难度等,为救援队伍制定合理的行动方案,提升救援效率。:泥石流是山区常见的地质灾害之一,通过模拟分析可以评估某一地区发生泥石流的风险,指导土地使用规划、人口迁移等决策。

weixin_43481439的博客 1320

根据后序和中序遍历输出先序遍历

作者 DS课程组单位 浙江大学本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。

m0_73794536的博客 594

Java utgard连接OPC问题记录

怎么开启是个问题, 是在引入openscada的jar包里的Sever类中修改,这就犯难了, 其实没必要, 只要在自己项目下创建与openscada中Server类相同的路径,并复制Server过去改就行了, 项目会优先使用本地类,像下面这样。奇迹发生了,0x00000005解决. 注: 我的安全补丁编码跟网上查到的并不一样, 所以最初没有删,后来全都删了就好了,这个编码规则咱也不知道.安全日志中记录用户登录行为, OPC通信需要使用Windows系统用户进行登录, 如果用户名密码错误这里会有相关记录.

wangxin1949的博客 2840

数据结构--二叉树遍历

二叉树有三种遍历方式: 先序遍历:根左右 中序遍历:左根右 后序遍历:左右根 由二叉树的遍历序列推导树的构造时,必须已知先/后序列和中序遍历序列; 由树的先序遍历或后序遍历序列可以推出树的根; 由树的中序遍历序列可以推出树的左子树成员和右子树成员,然后可以递归的推出子树的根和子树。 例:已知先序序列为ABDGCEF,中序序列为DGBAECF,构造二叉树。 ABDGCEF 为先序 根左右 -...

小刘爱搬砖 1565

Flowable前端可视化配置动态审批人:告别硬编码,实现部门领导、项目经理自动分配

本文详细介绍了Flowable前端可视化配置动态审批人的全栈解决方案,通过表达式分配、监听器分配等动态审批人机制,实现从硬编码到智能分配的转变。文章提供了前端扩展bpmn-js设计器、后端动态解析实现方案及性能优化建议,帮助企业应对组织架构变动,提升审批流程的灵活性和效率。

weixin_30352645的博客 472

iOS安全攻防:从DarkSword漏洞利用链剖析到纵深防御实践

在移动安全领域,内存破坏漏洞(如堆溢出、释放后使用)是攻击者突破系统防线、实现远程代码执行和权限提升的核心技术原理。这些漏洞利用技术通过精心构造的攻击链,能够绕过沙箱、地址空间布局随机化等安全机制,最终威胁设备内核安全,其技术价值在于揭示了系统安全架构的潜在薄弱环节。在iOS应用开发与系统防护中,理解此类攻击链对于构建有效的纵深防御体系至关重要。应用场景涵盖从安全编码、网络通信加固到运行时应用程序自保护等多个层面。本文以DarkSword工具包为例,深入拆解其利用链,并分享如何通过内存安全实践、代码签名强化

weixin_30357231的博客 479

7-2 根据后序和中序遍历输出先序遍历 (25 分)

7-2 根据后序和中序遍历输出先序遍历 (25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。 输入格式: 第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。 输出格式: 在一行中输出Preorder: 以及该树的先序遍历结果。数字间有1个空格,行末不得有多余空格。 输入样例: 7 2 3 1 5 7 6 4 1 2 3 4 5 6 7 输出样例: Preord

weixin_45928161的博客 1970

ubuntu18.04 升级Ubuntu 20.04

Ubuntu 18.04升级20.04指南:首先备份重要数据,更新当前系统并检查新版本可用性(sudo do-release-upgrade -c)。确认升级后执行sudo do-release-upgrade,按提示完成操作。升级过程需耐心等待,完成后重启系统。注意:升级前务必备份数据,确保系统为最新版本,以降低风险。升级后即可使用Ubuntu 20.04系统。

https://ophome.blog.youkuaiyun.com,在IT行业有多年的工作经验,尤其是在Python、Linux、负载均衡、Nginx和服务器运维等领域。 1058

PTA7-2 根据后序和中序遍历输出先序遍历

递归思想,以及对后序遍历和中序遍历的特点分析运用

bigBbug的博客 380

①电子产品拆解分析-暖手宝

电子产品拆解分析-暖手宝 ①按键三档调温暖手; ②长按进入氛围呼吸灯; ③安卓接口充电以及支持USB输出;

a6662580的博客 5294

《使用R包ggtree进行进化树的绘制与美化》

都2023年了,才去使用Y叔早在2018年发表的进化树构建利器——ggtree。刚好最近在做基因家族分析,进化树的构建与美化确实有点麻烦,先前使用MEGA11进行了绘制,但图像简陋,于是导出了nwk文件,准备使用其他软件进行美化。这里,我使用ggtree包进行操作。

沉香best的博客 6894

7-1 根据后序和中序遍历输出先序遍历 (25 分)

7-14 根据后序和中序遍历输出先序遍历 (25 分) 本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。 输入格式: 第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。 输出格式: 在一行中输出Preorder:以及该树的先序遍历结果。数字间有1个空格...

Harington的博客 3670

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

7-7 根据后序和中序遍历输出先序遍历 (25分)【板子】

1、数组a在函数中的传递:int *a 2、根结点为后序遍历的最后一个 3、在中序遍历中查找根结点 4、//对左子树来查找根结点 //对右子树来查找根结点 #include <bits/stdc++.h> using namespace std; /* 2 3 1 5 7 6 4 后序 1 2 3 4 5 6 7 中序 1、root=4 2、i=3 3、getpre(a,b,3) 4、getpre(a+3,b+3+1,7-3-1) 4、get.

Rita的博客 913

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

根据后序和中序遍历输出前序遍历

7-2 根据后序和中序遍历输出前序遍历本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的前序遍历结果。

2301_80089510的博客 350

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

输入前序遍历、中序遍历输出后续遍历

//二叉树  输入前序遍历和中序遍历   输出后续遍历 // by nby #include using namespace std; void ACore(int *startpreorder, int *endpreorder, int *startinorder, int *endinorder) { if (startpreorder == endpreorder)

行走 750

浙大版《数据结构(第2版)》题目集练习4.1 根据后序和中序遍历输出先序遍历 (25分)

思路,通过递归构建一个二叉树,然后先序序列输出答案。 #include<stdio.h> #include<stdlib.h> #define max 30 //构建二叉树 typedef struct Node *Tree; struct Node{ int data; Tree left; Tree right; }; Tree createtree(int a[],int b[],int n);//构建二叉树 void print(Tree b);

MortyC99430的博客 367

必做】7-6 根据后序和中序遍历输出前序遍历分数 25全屏浏览切换布局作者 DS课程组单位 浙江大学

本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的前序遍历结果。

2301_80040876的博客 874

树的前序遍历、中序遍历、后序遍历详解

1.前序遍历 对于当前节点,先输出该节点,然后输出他的左孩子,最后输出他的右孩子。以上图为例,递归的过程如下: (1):输出 1,接着左孩子; (2):输出 2,接着左孩子; (3):输出 4,左孩子为空,再接着右孩子; (4):输出 6,左孩子为空,再接着右孩子; (5):输出 7,左右孩子都为空,此时 2 的左子树全部输出,2 的右子树为空,此时 1 的左子树全部输出,接着 1 的右子树; (6):输出 3,接着左孩子; (7):输出 5,左右孩子为空,此时 3 的左子树全部输出,3 的右子树为空,至

尘埃飞舞 2万+
上一篇: 7-1 还原二叉树
下一篇: 7-3 玩转二叉树
四零八之王
博客等级 码龄4年 24粉丝 14原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四零八之王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值