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/ ...
 
随机推荐
- 通过Anuglar Material串串学客户端开发  - NodeJS模块机制之Module.Exports
			
module.exports 前文讲到在Angular Material的第二个编译文件docs/gulpfile.js中却看到了一个奇怪的东西module.exports那么module.expor ...
 - [汇编] C语言中嵌入汇编
			
>_<" 下面是在C语言中嵌入汇编的例子,下面是三点要注意的~ 1.内联式汇编 2._asm关键字 3.并不是所有中断都能被支持 #include<iostream> ...
 - [WinAPI] API 14 [获取、设置文件属性和时间]
			
>_< 为了获取文件属性,用户可以使用GetFileAttributes与GetFileAttributesEx函数. GetFileAttributesEx函数除了返回文件属性外,还返回 ...
 - docker学习笔记一:基本安装和设置容器静态ip
			
docker是一个lxc升级版的容器类虚拟环境,具有快速部署,灵活,易迁移的虚拟机模式,现在各大公司已经开始广泛使用为了自己方便学习linux,需要多台虚拟机环境,但是vmware开启多台虚拟机时需要 ...
 - Leetcode 70 Climbing Stairs 递推
			
其实就是斐波那契数列 参考dp[n] = dp[n-1] +dp[n-2]; class Solution { public: int climbStairs(int n) { ; ; ; ; i & ...
 - linux进程监控,monitor脚本
			
由于服务器上一些进程莫名的挂掉,需要些一个monitor的bash脚本来监控这些进程: #! /bin/bash #chkconfig info ### BEGIN INIT INFO # Provi ...
 - 使用MFC提供的Http类下载和上传文件
			
1.下载文件 Download(const CString& strFileURLInServer, //待下载文件的URL const CString & strFileLocalF ...
 - C# 传入引用类型的参数 返回值是否发生变化
			
前一段时间做项目是,一YY说如果一个方法的参数是引用类型,那么在这个方法里面所做的所有的修改再方法调用后应该有体现.事实是这样的吗? 先看code 和运行结果: 运行结果 方法SetPersonInf ...
 - 【Android】Android 移动应用数据到SD
			
[Android]Android 移动应用数据到SD 在应用的menifest文件中指定就可以了,在 <manifest> 元素中包含android:installLocation 属性, ...
 - SFTP+OpenSSH+ChrootDirectory设置
			
账户设置 SFTP的账户直接使用Linux操作系统账户,我们可以用useradd命令来创建账户. 首先建立3个要管理的目录: 1 2 3 mkdir /home/sftp/homepage mkd ...