zoukankan      html  css  js  c++  java
  • hdu 3999 The order of a Tree

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    typedef struct node *tree;
    typedef struct node
    {
        tree left;
        tree right;
        int data;
    };
    bool f;
    tree in(int t,tree p)
    {
        if(!p)
        {
            p=(tree)malloc(sizeof(struct node));
            p->data=t;
            p->left=p->right=NULL;
        }
        else
        {
            if(t<p->data)
            {
                p->left=in(t,p->left);
            }
            else if(t>p->data)
            {
                p->right=in(t,p->right);
            }
        }
        return p;
    }
    void preorder(tree p)//先序遍历
    {
        if(p)
        {
            if(f) printf(" ");
            else f++;
            printf("%d",p->data);
            preorder(p->left);
            preorder(p->right);
        }
    }
    int main()
    {
        int n,i,t;
        while(~scanf("%d",&n))
        {
            tree root=NULL;
            for(i=0;i<n;i++)
            {
                scanf("%d",&t);
                root=in(t,root);
            }
            f=0;
            preorder(root);
            printf("
    ");
        }
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    链表-(1)
    爬虫(10-3)验证码图片识别
    爬虫10-2(多线程爬虫)
    分布式爬虫系统的架构(19)
    pipenv管理Python虚拟环境
    peewee-async集成到tornado
    Python3笔记051
    Python3笔记050
    Python3笔记049
    Python3笔记048
  • 原文地址:https://www.cnblogs.com/xryz/p/4848035.html
Copyright © 2011-2022 走看看