题解 | #二叉树遍历#

二叉树遍历

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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110;
typedef struct TNode {
    char data;
    struct TNode* lchild;
    struct TNode* rchild;
}TNode,*Tree;
string str;
Tree Build(int &position){
    char c = str[position++];
    if(c=='#'){
        return NULL;
    }
    Tree root=(Tree)malloc(sizeof(TNode));
    root->data=c;
    root->lchild = Build(position);
    root->rchild=Build(position);


    return root;
}

void InOrder(Tree root){
    if(root == NULL) return;
    InOrder(root->lchild);
    printf("%c ",root->data);
    InOrder(root->rchild);

}
int main() {
    cin >> str;
    int position = 0;
    Tree root= Build(position);
    InOrder(root);

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

我是小红是我:学校换成中南
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务