【题解搬运】PAT_A1020 树的遍历
题目
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
题解
这题搞了我一个小时!!!好气啊,我们好好梳理一下这条题目。其实摸鱼了半个小时
给定一棵二叉树的后根遍历和中根遍历,求其前根遍历。
如何做呢?这里一定要有递归的思想。我们设定一个 int makeTree(int l,int r); 函数返回的是这棵树的根在动态数组(vector)的位置(感觉指针更好写);l和r是它元素的范围。那么显然main函数调用的是 makeTree(,n-) 。后面就很好写了!(一点也不
定义一个cur变量,它指的是下一次调用makeTree函数时的根节点,rootIdx则为目前树的根节点。那么后根遍历都知道是右→左→根,那么我们根据后根遍历构建树,根据中根遍历找左右子树,找到左右子树的边界以后递归调用makeTree函数即可。特别地,如果l>r,说明此树不存在;如果l==r,说明此节点是叶子节点,一一特判即可。这条题目以前我就不会写,今天放在这里,警戒自己学习一个。
特别地,以前还看过这样类似的题型:已知前根遍历和后根遍历,求有几种可能的(二叉)树。我们之后讨论一下。
代码
#include <bits/stdc++.h>
using namespace std;
#define f1(x,y) for(int x=1;x<=y;++x)
#define f0(x,y) for(int x=0;x!=y;++x)
#define bf1(x,y,z) for(int x=y;x>=z;--x)
#define bf0(x,y,z) for(int x=y;x!=z;--x)
typedef long long ll;
typedef unsigned long long ull;
int n;
vector<int> inOrd,posOrd;
int cur;
struct Tree
{
int id;
int lp,rp;
Tree(int _i):id(_i),lp(-1),rp(-1) {}
};
vector<Tree> t;
int makeTree(int l,int r)
{
if(l>r) return -1;
int nowIdx=cur--;
int rootIdx=0;
for(int i=0;i!=inOrd.size();++i)
if(inOrd[i]==posOrd[nowIdx])
{
rootIdx=i;break;
}
t.push_back(Tree(inOrd[rootIdx]));
int np=t.size()-1;
if(l==r)
t[np].rp=t[np].lp=-1;
else
{
int tmp=makeTree(rootIdx+1,r);
t[np].rp=tmp;
tmp=makeTree(l,rootIdx-1);
t[np].lp=tmp;
}
return np;
}
int main()
{
cin>>n;
int x;
f0(i,n)
{
cin>>x;
posOrd.push_back(x);
}
f0(i,n)
{
cin>>x;
inOrd.push_back(x);
}
cur=n-1;
int root=makeTree(0,n-1);
queue<int> q;
q.push(root);
while(!q.empty())
{
int tr=q.front(); q.pop();
if(tr==root) printf("%d",t[tr].id);
else printf(" %d",t[tr].id);
if(t[tr].lp!=-1) q.push(t[tr].lp);
if(t[tr].rp!=-1) q.push(t[tr].rp);
}
printf("\n");
/* for(int i=0;i!=5;++i)
{
printf("%d: %d<-self->%d\n",t[i].id,((t[i].lp==-1)?-1:(t[t[i].lp].id)),((t[i].rp==-1)?-1:(t[t[i].rp].id)));
}*/
return 0;
}
【题解搬运】PAT_A1020 树的遍历的更多相关文章
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- YTU 3023: 树的遍历
原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: 2 题 ...
- 团体程序设计天梯赛-练习集L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- leetcode404-----简单的树的遍历
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- pat L2-006. 树的遍历
L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...
- L2-006. 树的遍历
题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...
- js实现对树深度优先遍历与广度优先遍历
深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树
L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...
随机推荐
- HDU 5687 Problem C 【字典树删除】
传..传送:http://acm.hdu.edu.cn/showproblem.php?pid=5687 Problem C Time Limit: 2000/1000 MS (Java/Others ...
- Android学习笔记_76_Android ProgressBar 进度条
android 进度条的样式 例1:(默认样式(中等圆形))Xml代码 <ProgressBar android:id="@+id/progressBar1" ...
- python—迭代器
迭代器 这些可以直接作用于for循环的对象统称为可迭代对象:Iterable. 可以使用isinstance()判断一个对象是否是Iterable对象: >>> from colle ...
- Asset Store 下载的package存在什么地方?
发现从Asset store下载的packages都不知道放在了什么地方 Windows 7,C:\Users\<username>\AppData\Roaming\Unity\Asset ...
- 【转】opatch学习
[转自:https://yq.aliyun.com/articles/28007,仅作学习用途] Opatch 是oracle公司开发的安装,卸载,检测patch冲突的工具,管理oracle所有已经安 ...
- selenium之Xpath定位
1. 绝对定位: driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从 ...
- the “inner class” idiom
有些时候我们需要upcast为多种类型,这种情况下除了可以使用multiply inherits还可以inner class. 以下为例子: //: C10:InnerClassIdiom.cpp / ...
- JavaScript 基础(五) 函数 变量和作用域
函数定义和调用 定义函数,在JavaScript中,定义函数的方式如下: function abs(x){ if(x >=0){ return x; }else{ return -x; } } ...
- Git基本使用及工具
好久没用git管理代码了,最近忙着要实习,一直在看面试题,后天入职了,就提前再复习一下git吧. git比较方便的两个网站,如果你想逼格高就用GitHub(https://github.com/),如 ...
- MyBatis模糊查询的三种拼接方式
1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%'); 2. 使用 ${...} ...