博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3999 The order of a Tree
阅读量:7235 次
发布时间:2019-06-29

本文共 2003 字,大约阅读时间需要 6 分钟。

先建立一颗二叉搜索树,然后先序遍历一下,就可以出答案了。

我用了比较奇葩的写法。。。二叉搜索树结构体模拟了一下。先序遍历用的DFS。。。。。。

#include
#include
struct abc{ int left; int right; int date;}node[100010];int flag[100010];int ans[100010];int n, tot;void dfs(int wei){ if (node[wei].left != -1 && flag[node[wei].left] ==0) { flag[node[wei].left] = 1; ans[tot] = node[node[wei].left].date; tot++; dfs(node[wei].left); } if (node[wei].right != -1 && flag[node[wei].right] == 0) { flag[node[wei].right] = 1; ans[tot] = node[node[wei].right].date; tot++; dfs(node[wei].right); }}int main(){ int i, k; while (~scanf("%d", &n)) { memset(flag, 0, sizeof(flag)); for (i = 0; i <= n; i++) node[i].date = -1, node[i].left = -1, node[i].right = -1; scanf("%d", &k); tot = 1; node[1].date = k; int nodesum = 2; for (i = 2; i <= n; i++) { scanf("%d", &k); int nodexun = 1; while (1) { if (k > node[nodexun].date) { if (node[nodexun].right == -1) { node[nodexun].right = nodesum; node[nodesum].date = k; nodesum++; break; } else nodexun = node[nodexun].right; } else { if (node[nodexun].left == -1) { node[nodexun].left = nodesum; node[nodesum].date = k; nodesum++; break; } else nodexun = node[nodexun].left; } } } ans[tot] = node[1].date; tot++; flag[1] = 1; dfs(1); for (i = 1; i <= n; i++) { if (i

 

转载于:https://www.cnblogs.com/zufezzt/p/4438834.html

你可能感兴趣的文章
Uniform Resource Identifier
查看>>
UWP 浏览本地图片及对图片的裁剪
查看>>
Xshell高级后门完整分析报告
查看>>
一起谈.NET技术,数组排序方法的性能比较(3):LINQ排序实现分析
查看>>
括号自动补全
查看>>
Redis复制与可扩展集群搭建
查看>>
Express入门教程:一个简单的博客
查看>>
一份React-Native学习指南-感谢分享
查看>>
在Linux中使用VS Code编译调试C++项目
查看>>
JS创建select的optgroup
查看>>
win7无法ping通原因
查看>>
Javascript综合手册
查看>>
宏定义中使用do{}while(0)的好处 (转载)
查看>>
div 分页
查看>>
POJ_2392 Space Elevator(多重背包)
查看>>
常用user agent
查看>>
html中的caption是什么用
查看>>
Nova创建虚拟机的底层代码分析
查看>>
linux权限及ntfs文件系统权限的知识
查看>>
编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)
查看>>