题解 | #二叉树遍历#

二叉树遍历

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

#include <iostream>
#include <string>

using namespace std;

struct Node {
    char c;
    Node* left = NULL;
    Node* right = NULL;
};

Node* buildTree(int& index, string str) {
    if (str[index] == '#') {
        index++;
        return NULL;
    } else {
        Node* p = new Node();
        p->c = str[index];
        index++;
        p->left = buildTree(index, str);
        p->right = buildTree(index, str);
        return p;
    }
}

void InOrder(Node* p) {
    if (p == NULL) {
        return ;
    } else {
        InOrder(p->left);
        printf("%c ", p->c);
        InOrder(p->right);
    }
}

int main() {
    string str;
    getline(cin, str);
    int index = 0;
    Node* root = buildTree(index, str);
    InOrder(root);
}

全部评论

相关推荐

AC鸽想进大厂:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
10-13 16:58
门头沟学院 Java
面了100年面试不知...:一周七天,一天去一家上班😍😍😍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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