题解 | #二叉树遍历#

二叉树遍历

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")

全部评论

相关推荐

安静的鲸鱼offer...:神仙级别hr,可遇不可求,甚至他可能也是突然有感而发。只能说遇上是件幸事。
秋招开始捡漏了吗
点赞 评论 收藏
分享
LZStarV:冲就好了,就算真的是字节也冲,面评脏了大不了等三四个月就淡了,而且等到那个时候实力进步了选择还多,何必拘泥于字节
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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