hdu3999 The order of a Tree

Posted myrtle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu3999 The order of a Tree相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999

题意:给一序列,按该序列插入二叉树,给出字典序最小的插入方法建相同的一棵树出来。即求二叉树的先序遍历。

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int v;
    node *left,*right;
}*root;
node* build(node *root,int v)
{
    if(root==NULL)
    {
        root=new node();
        root->v=v;
        root->left=root->right=NULL;
    }
    else if(v<=root->v)root->left=build(root->left,v);
    else root->right=build(root->right,v);
    return root;
}

int flag;
void dfs(node *root)
{
    if(flag==1)cout<<root->v,flag=0;
    else cout<<" "<<root->v;
    if(root->left!=NULL)dfs(root->left);
    if(root->right!=NULL)dfs(root->right); 
}
int main()
{
    int n;
    while(cin>>n)
    {
        flag=1;
        root=NULL;
        int tmp;
        for(int i=0;i<n;i++)cin>>tmp,root=build(root,tmp);
        dfs(root);
        cout<<endl;
    } 
    return 0;
} 

 

 

 

以上是关于hdu3999 The order of a Tree的主要内容,如果未能解决你的问题,请参考以下文章

hdu 3006 The Number of set(思维+壮压DP)

hdu-1492 The number of divisors(约数) about Humble Numbers---因子数公式

HDU - 3006 The Number of set(状态压缩位运算)

*Find the Number Occurring Odd Number of Times

number field is the union of the set of all strings and the set of all numbers. The set of things th

hdu 1010 Tempter of the Bone