首页 > 其他 > 详细

常用算法快速回顾

时间:2014-02-26 03:03:42      阅读:248      评论:0      收藏:0      [点我收藏+]

二叉树的遍历 同类题型:1078

笨办法直接建树:

bubuko.com,布布扣
#include <cstdio>
#include <cstring>

char str[105];

struct Node {
    Node *lChild;
    Node *rChild;
    char d;
} Tree[105];
int pos;
int cur;

Node* BuildTree(int e) {
    if (str[cur] == # || cur >= e) return NULL;
    Tree[pos].lChild = Tree[pos].rChild = NULL;
    Node* root = &Tree[pos++];
    root->d = str[cur];
    ++cur;
    root->lChild = BuildTree(e);
    ++cur;
    root->rChild = BuildTree(e);
    return root;
}

void PrintTree(const Node* rt) {
    if(rt == NULL) return;
    if(rt->lChild != NULL) PrintTree(rt->lChild);
    printf("%c ", rt->d);
    if(rt->rChild != NULL) PrintTree(rt->rChild);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    
    while (scanf("%s", str) != EOF) {
        pos = 0;
        cur = 0;
        Node* rt = BuildTree(strlen(str));
        PrintTree(rt);
        printf("\n");
    }

    return 0;
}
View Code

或者直接模拟:

 

进制转换 

常用算法快速回顾

原文:http://www.cnblogs.com/fripside/p/3567186.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!