HDU 3999 The order of a Tree 二叉树

#include <iostream>
#include <cstdlib>
#include <string.h>
#include <algorithm>
#define ss(a)  scanf("%d",&a)
#define ss64(a)  scanf("%I64d",&a)
using namespace std;

typedef struct Node
{
    struct Node *l;
	struct Node *r;
	int num;
}*node;

void creat(node &T,int num)             //此处的&的引用是关键
{
   if (T == NULL)
   {
       T = (node)malloc(sizeof(Node));
	   T->num = num;
	   T->l = NULL;
	   T->r = NULL;;
   }
   else
	   if (num < T->num)
	   {
		   creat(T->l,num);
	   }
	   else
	   {
		   creat(T->r,num);
	   }

}

void post_visit(node T,int x)
{
    if (T != NULL)
    {
		if(x)
		   cout<<T->num;
		else
		   cout<<" "<<T->num;
		post_visit(T->l,0);
		post_visit(T->r,0);
    }
}

int main()
{
	int i,n,m,sum,cnt,j;
	int num;
	node T=NULL;
	while(~ss(n))
	{
		for (i=0;i<n;i++)
        {
			 ss(num);
			 creat(T,num);
		}
        post_visit(T,1);
		cout<<endl;
	}
    return 0;
}

考察二叉树的创建以及先序遍历

The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1337    Accepted Submission(s): 675

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

Source

2011 Multi-University Training Contest 16 - Host
by TJU

时间: 2024-10-17 08:03:20

HDU 3999 The order of a Tree 二叉树的相关文章

hdu 3999 The order of a Tree

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree 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 beco

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

1 /****************************************************************** 2 题目: The order of a Tree(hdu 3999) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999 4 题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序 5 列,可以建立和原序列建立的二叉搜索树一样且这个序列 6 是字典序最小 7 算法: 二叉搜索树 8 思想: 对于一个

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/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1361    Accepted Submission(s): 695 Problem Description As we know,the shape of

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest

HDU 4718 The LCIS on the Tree(树链剖分)

Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

HDU 3999 二叉排序树

The order of a Tree Problem Description 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 no