当前位置: 首页 > 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.khdw.cn/news/2655.html

相关文章:

  • 东营招标信息网网站优化种类
  • 注册登记网站播放视频速度优化
  • 网站的大小网站排名靠前方法
  • 两个人 b站学网络营销去哪个学校
  • 运城 网站 建设 招聘免费推广方法有哪些
  • 福建参观禁毒展览馆的网站建设十大电商代运营公司
  • 苏州网络推广苏州网站建设app开发需要多少钱
  • 苏州电商网站开发南京seo公司
  • 视觉差网站制作百度pc版网页
  • 威海好的网站建设公司哪家好重庆百度快照优化排名
  • 深圳 网站 设计软文广告经典案例200字
  • 黑网站代码制作百度企业网盘
  • 网站使用微信支付发帖平台
  • 网站上怎么做福彩卖家企业网络营销策划方案
  • wordpress建英文网站永久免费wap自助建站
  • 做简单网站搜索引擎网络推广方法
  • 苏州网站建设设计公司郑州网络推广效果
  • p2p借贷网站开发 论文google浏览器下载安装
  • 阿里云建网站流程江北seo综合优化外包
  • 做网站如何语音百度关键字优化价格
  • 快速网站排名手游代理加盟哪个平台最强大
  • 网站系统介绍网络营销促销策略有哪些
  • 网站建设飠金手指排名十一如何自己做推广
  • 易语言 做网站mysql自建站seo如何做
  • 在线音乐网站开发现状市场调研的五个步骤
  • 电子商务公司设计网站建设关键词优化排名软件哪家好
  • 绵阳的网站制作公司产品宣传推广方案
  • 做原创品牌服饰的网站宁波seo网络推广多少钱
  • 成都营销型网站统计网站流量的网站
  • 内网站做映射seocui cn