题解 | 二叉树遍历

二叉树遍历

https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef

#include <iostream>
#include <string>
using namespace std;
string s;
typedef struct tree {
    char d;
    struct tree* l, * r;
}* t;
tree* fun(int& i) {//i值一直累计
    if (s[i] == '#')return NULL;
    tree* root = new tree();
    root->d = s[i];
    root->l = fun(++i);
    root->r = fun(++i);
    return root;
}
void order(tree* root) {
    if (root == NULL )return;
    order(root->l);
    cout << root->d << " ";
    order(root->r);

}
int main() {
    cin >> s;
    int i = 0;
    tree* root = fun(i);
    order(root);
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务