zoukankan      html  css  js  c++  java
  • 1020. Tree Traversals

    C++语言: Codee#25888
    001 /*
    002 +++++++++++++++++++++++++++++++++++++++
    003         author: chm
    004 +++++++++++++++++++++++++++++++++++++++
    005 */
    006
    007 #include <map>
    008 #include <set>
    009 #include <list>
    010 #include <queue>
    011 #include <cmath>
    012 #include <stack>
    013 #include <bitset>
    014 #include <cstdio>
    015 #include <cctype>
    016 #include <string>
    017 #include <vector>
    018 #include <cassert>
    019 #include <cstdlib>
    020 #include <cstring>
    021 #include <fstream>
    022 #include <sstream>
    023 #include <iomanip>
    024 #include <iostream>
    025 #include <algorithm>
    026
    027 using namespace std;
    028
    029 FILE*            fin         = stdin;
    030 FILE*            fout         = stdout;
    031 const int        max_size     = 100;
    032 typedef struct treenode
    033 {
    034     int num;
    035     struct treenode* left;
    036     struct treenode* right;
    037 } BTNode;
    038
    039
    040 int postorder[max_size], inorder[max_size];
    041 BTNode* CreateBT(int* post, int* in, int n, int m)
    042 {
    043     BTNode* s;
    044     int* p;
    045     int* q;
    046     int* maxp;
    047     int maxpost;
    048     int maxin;
    049     int k;
    050
    051     if(n <= 0)
    052         return NULL;
    053     maxpost = -1;
    054     for(p = in; p < in + n; ++p)
    055         for(q = post; q < post + m; ++q)
    056             if(*p == *q)
    057             {
    058                 k = q - post;
    059                 if(k > maxpost)
    060                 {
    061                     maxpost = k;
    062                     maxp = p;
    063                     maxin = p - in;
    064                 }
    065             }
    066     s = (BTNode* )malloc(sizeof(BTNode));
    067     s->num = post[maxpost];
    068     s->left = CreateBT(post, in, maxin, m);
    069     s->right = CreateBT(post, maxp + 1, n - maxin - 1, m);
    070
    071     return s;
    072 }
    073
    074 #define ONLINE_JUDGE
    075 int main()
    076 {
    077 #ifndef ONLINE_JUDGE
    078     freopen("d:\\in.txt", "r", stdin);
    079     freopen("d:\\out.txt", "w", stdout);
    080 #endif
    081     int n;
    082     scanf("%d", &n);
    083     for(int i = 0; i < n; ++i)
    084         scanf("%d", &postorder[i]);
    085     for(int i = 0; i < n; ++i)
    086         scanf("%d", &inorder[i]);
    087     BTNode* root = CreateBT(postorder, inorder, n, n);
    088     queue<BTNode*> treeque;
    089     treeque.push(root);
    090     while(!treeque.empty())
    091     {
    092         BTNode* tmp = treeque.front();
    093         treeque.pop();
    094         printf("%d", tmp->num);
    095         if(NULL != tmp->left)
    096             treeque.push(tmp->left);
    097         if(NULL != tmp->right)
    098             treeque.push(tmp->right);
    099         printf("%s", treeque.empty() ? "\n" : " ");
    100     }
    101
    102 #ifndef ONLINE_JUDGE
    103     fclose(stdout);
    104     system("start d:\\check.exe d:\\out.txt d:\\ans.txt");
    105 #endif
    106     return 0;
    107 }

    编辑器加载中...

  • 相关阅读:
    weblogic.xml
    LogAspect
    加注解时插入权限切面@EnableDataSecurity
    查询时根据权限更改sql
    web.xml
    log4j.properties
    jaxb解析xml
    sql 更新 批量更新 更新得到主键
    告诉maven,我真的不需要web.xml
    FLask中蓝图(用于分文件)
  • 原文地址:https://www.cnblogs.com/invisible/p/2408541.html
Copyright © 2011-2022 走看看