关于 string 实现先序中序还原二叉树
  • 板块灌水区
  • 楼主lzy20091001
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/9/19 23:20
  • 上次更新2023/11/2 19:02:11
查看原帖
关于 string 实现先序中序还原二叉树
932039
lzy20091001楼主2023/9/19 23:20

蒟蒻求助,书上写的是 C 风格字符串,不得不说十分简便,string 有没有什么简便的方法?是不是必须加一个变量记录字符串的开始位置

// 二叉树存储
struct Bnode
{
    char data;
    struct Bnode *lchild, *rchild;
};

// 先序、中序还原二叉树
Bnode *pre_mid_createtree(char *pre, char *mid, int len)
{
    if (len == 0)
        return NULL;
    char ch = pre[0];
    int index = 0;
    while (mid[index] != ch)
        index++;
    Bnode *t = new Bnode;
    t->data = ch;
    t->lchild = pre_mid_createtree(pre + 1, mid, index);
    t->rchild = pre_mid_createtree(pre + index + 1, mid + index + 1, len - index - 1);
}
2023/9/19 23:20
加载中...