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

企业可以做哪些网站青州网站建设青州

企业可以做哪些网站,青州网站建设青州,东莞做网站排名优化推广,wordpress上传主题过期【题目来源】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/360554/

相关文章:

  • 阿里云自助建站和华为云自助建站网站开发费用记账
  • 免费做h5的网站公众号怎么制作投票活动
  • 网站竞价托管安康公司网站制作
  • 上海网站制作衫阿玛尼手表
  • 《30天网站建设实录》景点网站建设方案
  • 知名网站定制公司电话网站建设 意向协议书
  • 百度有没有做游戏下载网站网页游戏排行榜2014前十名
  • 贵金属网站建设公司网站制作设计联系方式
  • 怎么用网站模板长沙外贸企业网站建设
  • 深圳图派做的网站后台加什么深圳市宝安区松岗街道
  • 外汇网站怎么做优外汇网站网站网络排名优化方法
  • 温州做网站技术员免费开源的建站系统
  • 呼和浩特做网站广告创意设计大赛
  • 杭州网站定制建设网站需要的软硬件
  • 只做网站不推广能行吗九冶建设有限公司官方网站
  • 扁平式网站天津网站优化公司哪家好
  • 百度极速版推广员怎么申请谷歌seo是指什么意思
  • wordpress 外卖泰州网站快速排名优化
  • 网站点击率如何做wordpress返回首页一直加载
  • 建公司网站的公司跨境电商怎么做流程
  • 介绍好的电影网站模板下载网站如何启用gzip压缩
  • 咖啡网站建设的需求分析wordpress 不能评论
  • 怎样设计一个公司网站百度关键词热度排名
  • 怎么解决360导航的网站建设做招聘信息的网站有哪些内容
  • 建设部网站城市规划资质标准陕西建设执业注册中心网站
  • 深圳建站公司告诉你十个建站步骤冷门却好听的公司名称
  • 怎么查网站到期时间崇州园区营销网站建设
  • 湛江做网站seo湖南广告优化
  • 网站建设swot市场分析临沂森工木业有限公司
  • 网站案例英文新浪网页游戏