题解 | #二叉树遍历#

二叉树遍历

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

#include <string>
#include <iostream>
using namespace std;

struct TreeNode
{
    char data;
    TreeNode *left;
    TreeNode *right;
    TreeNode(char c):data(c),left(NULL),right(NULL){}
};

TreeNode* build(const string & str1, const string  &str2);
void postorder(TreeNode * r);
int main(int nums ,char ** args)
{
    ios::sync_with_stdio(false);
    string str1,str2;
    while(cin>>str1>>str2)
    {
        TreeNode*r=build(str1,str2);
        postorder(r);
        cout<<endl;

    }

    return 0;
}
TreeNode* build(const string & str1, const string  &str2)
{
    if(str1.size()==0)
        {return NULL;}
    int pos=str2.find(str1[0]);
    TreeNode* r=new TreeNode(str1[0]);
    r->left=build(str1.substr(1,pos),str2.substr(0,pos));
    r->right=build(str1.substr(1+pos),str2.substr(1+pos));
    return r;
}
void postorder(TreeNode * r)
{
    if(r==NULL) return;
    postorder(r->left);
    postorder(r->right);
    cout<<r->data;
}

全部评论

相关推荐

码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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