当前位置: 首页 > news >正文

网站管理后台下载网页游戏排行榜开服时间

网站管理后台下载,网页游戏排行榜开服时间,网站运营维护中需要用到什么服务器,微信公众平台注册平台【题目来源】https://www.luogu.com.cn/problem/B3642【题目描述】 有一个 n(n≤10^6) 个结点的二叉树。给出每个结点的两个子结点编号(均不超过 n),建立一棵二叉树(根结点的编号为 1),如果是叶子结点&…

【题目来源】
https://www.luogu.com.cn/problem/B3642

【题目描述】
有一个 n(n≤10^6) 个结点的二叉树。给出每个结点的两个子结点编号(均不超过 n),建立一棵二叉树(
根结点的编号为 1),如果是叶子结点,则输入 0 0。
建好树这棵二叉树之后,依次求出它的前序、中序、后序列遍历。

【输入格式】
第一行一个整数 n,表示结点数。
之后 n 行,第 i 行两个整数 l、r,分别表示结点 i 的左右子结点编号。若 l=0 则表示无左子结点,r=0 同理。

【输出格式】
输出三行,每行 n 个数字,用空格隔开。
第一行是这个二叉树的前序遍历。
第二行是这个二叉树的中序遍历。
第三行是这个二叉树的后序遍历。

【输入样例】
7
2 7
4 0
0 0
0 3
0 0
0 5
6 0

【输出样例】
1 2 4 3 7 6 5
4 3 2 1 6 5 7
3 4 2 5 6 7 1

【算法分析】
● 结构体方法,简单易懂。
● 链式前向星方法,复杂烧脑。
链式前向星:https://blog.csdn.net/hnjzsyjyj/article/details/126474608

【算法代码一:结构体方法】

#include <bits/stdc++.h>
using namespace std;const int maxn=1e6+5;struct Tree {int le,ri;
} tr[maxn];void pre(int x) {cout<<x<<" ";int le=tr[x].le;int ri=tr[x].ri;if(le) pre(le);if(ri) pre(ri);
}void in(int x) {int le=tr[x].le;int ri=tr[x].ri;if(le) in(le);cout<<x<<" ";if(ri) in(ri);
}void post(int x) {int le=tr[x].le;int ri=tr[x].ri;if(le) post(le);if(ri) post(ri);cout<<x<<" ";
}int main() {int head=1;int n;cin>>n;for(int i=1; i<=n; i++) {cin>>tr[i].le>>tr[i].ri;}pre(head),cout<<endl;in(head),cout<<endl;post(head),cout<<endl;return 0;
}/*
in:
7
2 7
4 0
0 0
0 3
0 0
0 5
6 0out:
1 2 4 3 7 6 5
4 3 2 1 6 5 7
3 4 2 5 6 7 1
*/


【算法代码二:链式前向星方法】

#include <bits/stdc++.h>
using namespace std;const int maxn=1e6+5;
const int maxm=maxn<<1;
int h[maxn],e[maxm],ne[maxm],idx;void add(int a,int b) {e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}void pre(int u) {cout<<u<<" ";for(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(j!=0) pre(j);}
}void in(int u) {int v1=0, v2=1;for(int i=h[u]; i!=-1; i=ne[i]) {v1++;int j=e[i];if(j==0) continue;if(v1==2) cout<<u<<" ", v2=0;in(j);}if(v2) cout<<u<<" ";
}void post(int u) {for(int i=h[u]; i!=-1; i=ne[i]) {int j=e[i];if(j!=0) post(j);}cout<<u<<" ";
}int main() {memset(h,-1,sizeof h);int head=1;int n;cin>>n;for(int i=1; i<=n; i++) {int le,ri;cin>>le>>ri;//Because it's head insertion,//so insert the right side firstadd(i,ri);add(i,le);}pre(head),cout<<endl;in(head),cout<<endl;post(head),cout<<endl;return 0;
}/*
in:
7
2 7
4 0
0 0
0 3
0 0
0 5
6 0out:
1 2 4 3 7 6 5
4 3 2 1 6 5 7
3 4 2 5 6 7 1
*/




【参考文献】
https://blog.csdn.net/qq_39456436/article/details/138681903
https://blog.csdn.net/hnjzsyjyj/article/details/127290036






 

http://www.yayakq.cn/news/869472/

相关文章:

  • 福州专业网站制作网页制作基础教程内容
  • wordpress 找不到主题做网站seo优化总结
  • 手机百度网站建设seo文章推广
  • 网页网站设计公司广州网络科技有限公司
  • 制作一个网站大概要多少钱重庆网站建设公司价钱
  • 佛山自己网站建设dw-focus wordpress主题
  • 专做女鞋的网站wordpress有游客注册帐号功能
  • 二级学院网站建设自评报告深圳最新消息
  • 网站开发亿玛酷信赖wordpress进后台
  • 内蒙古呼和浩特网站建设wordpress首页轮换图片在哪里设置
  • 网站标题怎么修改电子商务网站建设技术
  • 广东网站建设需要多少钱wordpress just
  • 昆明网站建设时间类似wordpress的建站系统
  • 济南网站建设行知科技不错a乌兰察布盟建设银行网站
  • 哪家网站设计公司好wordpress更新主机名
  • dede后台做两个网站Wordpress 充值 卡密
  • 阿里云带宽5m能做什么网站深圳网站开发技术
  • 公司网站怎么备案找人做菠菜网站需要多少钱
  • 学校网站代码模板asp.net网站开发试题
  • 搭建免费个人网站2022广告设计公司
  • 怎样制作印章seo网络推广是干嘛的
  • 企业网站成品源码福田住房和建设局官网
  • wordpress美化插件漯河seo
  • 网站开发教学文章如何建设网站挣钱
  • 网站被k怎么解决新网网站空间购买
  • 网站设计要学什么建工之家
  • 全球设计网站有哪些快递企业网站建设设计方案范例
  • wordpress 视频站网站配资公司网站
  • 万网网站制作成都优化网站哪家公司好
  • asp网上书店网站开发青岛网站设计怎么选