zoukankan      html  css  js  c++  java
  • HDU 3999 The order of a Tree (排序二叉树的先序遍历)

    题目

    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

    思路

    给你一个序列,让你根据这个序列建立排序二叉树,然后问你一个序列,这个序列最后建立的排序二叉树和我给出的是一颗,且字典序最小。那么由排序二叉树的性质我可以知道,先序遍历的序列一定是字典序最小的。

    Code

    #include<vector>
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    void check_max (int &a,int b) {a=max (a,b);}
    void check_min (int &a,int b) {a=min (a,b);}
    vector <int> v;
    int n;
    struct node {
    	int data;
    	node *leftchild,*rightchild;
        node () {
    		leftchild=NULL;
    		rightchild=NULL;
    	}
    };
    
    void insert (node *&root,int data) {
    	if (root==NULL) {
    		root=new node ();
    		root->data=data;
    		return ;
    	}
    	else if (root->data==data) return ;
    	else if (data>root->data) insert (root->rightchild,data);
    	else if (root->rightchild,data) insert (root->leftchild,data);
    	return ;
    }
    
    node * build () {
    	node *root=NULL;
    	for (auto it:v) {
    		insert (root,it);
    	}
    	return root;	
    }
    
    void pre_travel (node *root,int flag) {
    	if (root==NULL) return ;
    	if (flag==1) printf ("%d",root->data);
    	else printf (" %d",root->data);
    	pre_travel (root->leftchild,++flag);
    	pre_travel (root->rightchild,++flag);
    	return ;
    }
    
    int main () {
        while (scanf ("%d",&n)!=EOF) {
    		v.clear ();
    		for (int i=1;i<=n;i++) {
    			int x; scanf ("%d",&x);
    			v.push_back (x);
    		}
    		node *root=build ();
    		pre_travel (root,1);
    		printf ("
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    阿里HBase高可用8年“抗战”回忆录
    Service Mesh 初体验
    阿里云HBase推出普惠性高可用服务,独家支持用户的自建、混合云环境集群
    Ververica Platform-阿里巴巴全新Flink企业版揭秘
    深度 | 带领国产数据库走向世界,POLARDB底层逻辑是什么?
    AI加持的阿里云飞天大数据平台技术揭秘
    Nacos 常见问题及解决方法
    数据上云,应该选择全量抽取还是增量抽取?
    一文带你了解 Flink Forward 柏林站全部重点内容
    Oracle数据库中序列(SEQUENCE)的用法详解
  • 原文地址:https://www.cnblogs.com/hhlya/p/14184145.html
Copyright © 2011-2022 走看看