题解 | #二叉树遍历#

二叉树遍历

https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce

#include <cstddef>
#include <iostream>
#include <cstring>
using namespace std;
struct Bitree{
    char data;
    struct Bitree* lchild;
    struct Bitree* rchild;
    Bitree(char x):data(x),lchild(NULL),rchild(NULL){}
};
Bitree* buildT(string front,string mid){
    if(front==""||mid=="") return NULL;
    Bitree* r=new Bitree(front[0]);
    char n=front[0];
    int loc=mid.find(n);
    r->data=n;
    r->lchild=buildT(front.substr(1,loc), 
    mid.substr(0,loc));
    r->rchild=buildT(front.substr(loc+1), 
    mid.substr(loc+1));
    return r;
}
void backorder(Bitree* root){
    if(root==NULL) return;
    backorder(root->lchild);
    backorder(root->rchild);
    cout<<root->data;
}
int main() {
    string f,m;
    while(cin>>f>>m){
        Bitree* r= buildT(f, m);
        backorder(r);
        cout<<endl;

    }
}

全部评论

相关推荐

04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务