HDU 3999 The order of a Tree
The order of a Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 845 Accepted Submission(s): 461
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.
1 3 4 2
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <stack>
- using namespace std;
- typedef struct node
- {
- int data;
- node *lchild;
- node *rchild;
- node()
- {
- lchild = rchild = NULL;
- }
- }TreeNode;
- void CreateTree(TreeNode *&pRoot, int data)
- {
- if (pRoot == NULL)
- {
- pRoot = new TreeNode;
- pRoot->data = data;
- }
- else
- {
- if (data > pRoot->data)
- {
- CreateTree(pRoot->rchild, data);
- }
- else
- {
- CreateTree(pRoot->lchild, data);
- }
- }
- }
- void PreOrder(TreeNode *pRoot)
- {
- int nCount = ;
- if (pRoot == NULL)
- {
- return;
- }
- stack<TreeNode*> Stack;
- Stack.push(pRoot);
- do
- {
- TreeNode *p = Stack.top();
- Stack.pop();
- if (nCount == )
- {
- printf("%d", p->data);
- nCount++;
- }
- else
- {
- printf(" %d", p->data);
- nCount++;
- }
- if (p->rchild != NULL)
- {
- Stack.push(p->rchild);
- }
- if (p->lchild != NULL)
- {
- Stack.push(p->lchild);
- }
- } while (!Stack.empty());
- }
- int main()
- {
- int n, num;
- scanf("%d", &n);
- TreeNode *pRoot = NULL;
- for (int i = ; i < n; i++)
- {
- scanf("%d", &num);
- CreateTree(pRoot, num);
- }
- PreOrder(pRoot);
- printf("\n");
- return ;
- }
HDU 3999 The order of a Tree的更多相关文章
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- HDU 3999 The order of a Tree (先序遍历)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3999 The order of a Tree 二叉搜索树 BST
建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...
- hdu3999-The order of a Tree (二叉树的先序遍历)
http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 3999 二叉排序树
The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- hdu3999The order of a Tree (二叉平衡树(AVL))
Problem Description As we know,the shape of a binary search tree is greatly related to the order of ...
- hdu 4670 Cube number on a tree(点分治)
Cube number on a tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
随机推荐
- jenkins2 pipeline插件的10个最佳实践
jenkins pipeline的10个最佳实践. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd ...
- Node.js 爬虫,自动化抓取文章标题和正文
持续进行中... 目标: 动态User-Agent模拟浏览器 √ 支持Proxy设置,避免被服务器端拒绝 √ 支持多核模式,发挥多核CPU性能 √ 支持核内并发模式 √ 自动解码非英文站点,避免乱码出 ...
- SQL——字符串处理函数
1) ASCII Format:ASCII ( character_expression ) Function:返回表达式最左端字符的ASCII值. eg: select ASCII('abcdef' ...
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
一些重要的知识: mybais-spring.jar及其提供的API: SqlSessionFactoryBean: SqlSessionFactory是由SqlSessionFactoryBuild ...
- Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列
Atitit.提升软件稳定性---基于数据库实现的持久化 循环队列 环形队列 1. 前言::选型(马) 1 2. 实现java.util.queue接口 1 3. 当前指针的2个实现方式 1 1.1 ...
- Socket通信实例(C#)
SOCKET原理 一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息: ...
- Struts2入门3 深入学习
Struts2入门3 深入学习 处理结果和异常 前言: Struts学习的差不多了,还有最后的一点就收官了就是结果处理和异常处理.前面学习Struts主要围绕就是Action以及struts.xml配 ...
- Android中的IOC框架,完全注解方式就可以进行UI绑定和事件绑定
转载请注明出处:http://blog.csdn.net/blog_wang/article/details/38468547 相信很多使用过Afinal和Xutils的朋友会发现框架中自带View控 ...
- SourceTree - 正在检查源... When cloning a repository, "Checking Source" spins forever
I am trying to clone a repository, my OpenSSH is set up correctly and I can do everything fine in Gi ...
- jinkins在windows上的安装 配置C#编译
首先jinkins在windows上的安装就不说,安装只需要下载相应安装包就可以了,后有些时候经常需要修改端口号.修改如下: 然后重启jenkins服务 首次运行界面 个人建议插件按需安装. 建立一个 ...