HDU3999-The order of a Tree
The order of a Tree
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 64 Accepted Submission(s) : 34
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
1. insert a key k to a empty tree, then the tree become a tree with only one node;
2. insert a key k to a nonempty tree, if k is less than the root ,insert it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
Input
Output
Sample Input
41 3 4 2
Sample Output
1 3 2 4
#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;#define N 100005
int tree[N],l[N],r[N],a[N],num,flag,m;void fx(int k,int x)
{if( x <= tree[k] ){if(l[k] == -1) l[k] = flag;else fx(l[k],x);}else{if(r[k] == -1) r[k] = flag;else fx(r[k],x);}
}void f(int k)
{a[m++] = tree[k];if(l[k] != -1)f(l[k]);if(r[k] != -1)f(r[k]);
}
int main()
{int n,i,x,root;while(~scanf("%d",&n)){memset(l,-1,sizeof(l));memset(r,-1,sizeof(r));root = -1;flag = 0;for(i = 0; i< n; i++){scanf("%d",&x);if(root == -1)tree[++root] = x;else{tree[++flag] = x;fx(root,x);}}m = 0;f(root);for(i = 0; i < m -1; i++)printf("%d ",a[i]);printf("%d\n",a[m-1]);}return 0;
}
HDU3999-The order of a Tree相关推荐
- HDU3999 The order of a Tree【二叉搜索树 + 前序遍历】
The order of a Tree 题意: 给定一个二叉搜索树的插入顺序,要求输出建树之后的前序遍历. 题解: 连续第二天做到二叉搜索树了,真的吹爆这个算法,从二叉树啥也不懂,到简单搜索树直接秒杀 ...
- hdu3999 The order of a Tree(BST的建立)
http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给你一个序列可以构成一个二叉搜索树,求此二叉搜索树字典序最小的输入序列. 思路:这题只要明确一点就可 ...
- The order of a Tree hdu3999 二叉搜索树
题目链接: hdu3999 The order of a Tree 题意: 给一个二叉搜索树的序列,求输出可以构成这个二叉搜索树的且满足字典序最小的插入序列 找了一个比较具体描述怎么根据序列建立二叉搜 ...
- The order of a Tree(模拟BST的建树和访问)
题目链接 Problem Description As we know,the shape of a binary search tree is greatly related to the orde ...
- leetcode【102,103】Binary Tree Level Order Traversal Binary Tree Zigzag Level Order Traversal
问题描述(102): Given a binary tree, return the level order traversal of its nodes' values. (ie, from lef ...
- hdu 3999The order of a Tree
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3999 本题为简单二叉排序树,先按排序树创建树,然后先序遍历二叉树,输出的时候最后一个数字后面没有空格. ...
- HDU 3999 The order of a Tree
/*二叉树的插入,前序遍历. */#include<iostream> #include<cstring> #include<stdio.h> #include&l ...
- 01 二叉树的BFS(广度、层次或水平遍历实现)【Binary Tree 二叉树】
二叉树的遍历分为BFS和DFS两种大类 下面完整实现BFS遍历二叉树 * 例如二叉树* 1* / \* 2 3* /\* 4 5 BFS遍历结果:1-2-3-4-5 具体的代码实现: 方法一.采用递归 ...
- Linux进程浏览器htop安装与使用
htop 是一个 Linux 下的交互式的进程浏览器,可以用来替换Linux下的top命令.当前具有按树状方式来查看进程,支持颜色主题,可以定制等特性.其实htop是top的加强版,增加了很多功能. ...
最新文章
- C语言引用不同路径下的头文件的方法
- python json数据的转换
- Leaflet中使用awesome-markers插件显示带图标的marker
- SharpZipLib使用示例
- python matplotlib画折线图出现连线混乱_python使用matplotlib模块绘制多条折线图、散点图...
- tiny4412(用户手册及芯片手册)
- 如何保证消息队列的高可用?透彻分析源码
- 垂直和水平居中方法小结
- Dell Latitude 3490 使用 UEFI+GPT 安装 Win7 x64
- 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)BIJ,签到抽奖
- Cesium 鼠标单击和双击事件
- c语言 统计素数并求和
- Flash播放器常用参数设置_应用技巧
- 计算机毕业设计 SSM+Vue音乐播放网站系统 云音乐播放系统 付费音乐播放系统Java Vue MySQL数据库 远程调试 代码讲解
- 算法系列之算法学习书籍以及资料推荐
- 392高校毕业设计选题
- 人生在世,最大的笑话莫过于自作多情...
- C语言:有一个一维数组score,内放10个学生成绩,求平均成绩。
- 地面三维激光扫描仪在火灾现场调查取证中的应用
- 偏微分方程的MATLAB解法