• 九度oj 1184 二叉树遍历


    原题链接:http://ac.jobdu.com/problem.php?pid=1184

    简单的二叉树重建,遍历.

    如下:

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<vector>
     5 struct node{
     6     char key;
     7     node *ch[2];
     8     node(char d) : key(d) { ch[0] = ch[1] = NULL; }
     9 }*root = NULL;
    10 int n = 0;
    11 static int k = -1;
    12 char buf[110];
    13 void built(node *&p){
    14     k++;
    15     if (k > n) return;
    16     if (buf[k] != '#'){
    17         p = new node(buf[k]);
    18         built(p->ch[0]);
    19         built(p->ch[1]);
    20     }
    21 }
    22 void travle(node *x){
    23     if (x != NULL){
    24         travle(x->ch[0]);
    25         printf("%c ", x->key);
    26         travle(x->ch[1]);
    27     }
    28 }
    29 void _free(node *x){
    30     if (x != NULL){
    31         _free(x->ch[0]);
    32         _free(x->ch[1]);
    33         delete x;
    34     }
    35 }
    36 int main(){
    37 #ifdef LOCAL
    38     freopen("in.txt", "r", stdin);
    39     freopen("out.txt", "w+", stdout);
    40 #endif
    41     while (~scanf("%s", buf)){
    42         k = -1, root = NULL, n = strlen(buf);
    43         built(root);
    44         travle(root);
    45         _free(root);
    46         printf("
    ");
    47     }
    48     return 0;
    49 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Python基础四
    Python基础三
    Python基础二
    Python基础一
    JAVA测试
    国庆随笔
    ATM-JAVA程序 //程序有5处相同错误,找不出原因 转账功能没有实现,修改密码来不及实现了
    JAVA程序测试感受
    第八周
    第七周
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4477570.html
Copyright © 2020-2023  润新知