题目

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 树的遍历的更多相关文章

  1. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  2. YTU 3023: 树的遍历

    原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题 ...

  3. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  4. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  5. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  6. L2-006. 树的遍历

    题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...

  7. js实现对树深度优先遍历与广度优先遍历

    深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...

  8. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  9. 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树

    L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

随机推荐

  1. Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】

    任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory ...

  2. Angular 引用库

    <!doctype html> <html ng-app> <head> <script src="http://code.angularjs.or ...

  3. intellij idea下载安装以及创建项目(输出Hello World)

    主题一:下载 第一:可以百度下载 第二:直接进行官网链接下载地址:http://www.jetbrains.com/idea/ 第一步: 第二步: 下载完成后进行安装 主题二:安装 点击Next速度可 ...

  4. 简述 private、 protected、 public、 internal 修饰符的访问权限

    简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在该类的内部才可以访问. protected : 保护成员,该类内部 ...

  5. Restframework介绍

    1.REST介绍 REST与技术无关,它代表的是一种软件架构风格,全称Representational State Transfer,中文翻译为“表征状态转移” REST从资源的角度类审视整个网络,它 ...

  6. mysql获取汉字首字母函数

    DELIMITER ;;CREATE FUNCTION `GET_FIRST_PINYIN_CHAR`(PARAM VARCHAR(255)) RETURNS VARCHAR(2) CHARSET u ...

  7. IE浏览器关于ajax的缓存机制

    IE浏览器对于同一个URL只返回相同结果.因为,在默认情况下,IE会缓存ajax的请求结果.对于同一个URL地址,在缓存过期之前,只有第一次请求会真正发送到服务端.大多数情况下,我们使用ajax是希望 ...

  8. 设置Oracle数据库开机自启动

    1.oracle 用户下 修改$ORACLE_HOME/bin/dbstart   vim /home/oracle/database/product/12c/db_1/bin/dbstart 将OR ...

  9. vue.esm.js:578 [Vue warn]: Missing required prop

    问题: 解决: required: true,属性是,这个必须填写

  10. SPOJ8222 NSUBSTR - Substrings(后缀自动机)

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...