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

Problem Description
As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
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
There are multiple test cases in an input file. The first line of each testcase is an integer n(n <= 100,000),represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.
 
Output
One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.
 
Sample Input
4

1 3 4 2

 
Sample Output
1 3 2 4
题目大意:简历一颗二叉排序树,然后先序遍历。
#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的更多相关文章

  1. hdu 3999 The order of a Tree (二叉搜索树)

    /****************************************************************** 题目: The order of a Tree(hdu 3999 ...

  2. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

  3. 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 ...

  4. HDU 3999 The order of a Tree 二叉搜索树 BST

    建一个二叉搜索树,然后前序输出. 用链表建的,发现很久没做都快忘了... #include <cstdio> #include <cstdlib> struct Node{ i ...

  5. 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 ...

  6. HDU 3999 二叉排序树

    The order of a Tree Problem Description The shape of a binary search tree is greatly related to the ...

  7. 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 ...

  8. 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 ...

  9. 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/ ...

随机推荐

  1. 自定义Windows性能监视器

    Windows 性能监视器是一个很好用的自带监视工具,对于一些基本简单的监视需求可以轻松满足.本文主要总结了一下如何将自己应用中的一些性能数据暴露到性能监视器上方便管理. 什么?不知道什么是Windo ...

  2. [JAVA] java仿windows 字体设置选项卡

    想用java做一个像windows里一样的txt编辑软件,涉及到字体设置选项卡,在网上找了很久都没找到,就生气啦自己写一个,现在贴这里分享一下,下次再遇到这样的问题就不用自己亲自打代码啦! packa ...

  3. Neo4j:Data Model Transformation:From Relation To Graph

    Here are some tips that help you with the transformation: Each entity table is represented by a labe ...

  4. jenkins2 javahelloworld

    文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd   本文使用jenkins自动构建基于maven的 ...

  5. [BTS] Can't update the assembly.

    Error Message In BizTalk =================================== Failed to add resources to application. ...

  6. python获取当前日期前后N天或N月的日期

    # -*- coding: utf-8 -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime im ...

  7. select选择框内容左右移动添加删除栏(升级)

    先看一下之前的版本(10年前的作品了) 新版增加了拖动事件(双向及本列),双击左右自动移动,修正了算法性能更好: 也更新了如果姓名长度太长显示变形问题

  8. 使用 SELinux 和 Smack 增强轻量级容器

    http://www.bitscn.com/os/linux/200904/158771.html 安全 Linux 容器实现指南 轻量级容器 又称作 Virtual Private Servers ...

  9. ldr和adr在使用标号表达式作为操作数的区别

    ARM汇编有ldr指令以及ldr.adr伪指令,他门都可以将标号表达式作为操作数,下面通过分析一段代码以及对应的反汇编结果来说明它们的区别. ldr     r0, _start adr     r0 ...

  10. 使用jQuery开发一个响应式超酷整合RSS信息阅读杂志

    在线演示1 本地下载     申请达人,去除赞助商链接 如果大家喜欢阅读博客文章的话,可能都会使用RSS阅读器,今天这里我们将使用jQuery来开发一个响应式的RSS信息阅读应用,使用它你可以将你喜欢 ...