文章

75

粉丝

0

获赞

147

访问

8.7k

头像
二叉树遍历2 题解:c++
P1401 华中科技大学/中国科学院机试题
发布于2026年2月26日 20:30
阅读数 28

#include <bits/stdc++.h>
using namespace std;

typedef struct node{
    char data;
    node *left,*right;
}*Tree;

void CreateTree(Tree &t,string pre,string in){
    //每次确认一个根
    if(pre == ""){
        t = nullptr;
        return;
    }
    t = new node();
    t->data = pre[0];
    //把中序的分两半
    int index = in.find(pre[0]);
    string leftIn = in.substr(0,index);
    string rightIn = in.substr(index+1);
    //把先序的也分两半
    string leftPre = pre.substr(1,index);
    string rightPre = pre.substr(index+1);
    //递归
    CreateTree(t->left,leftPre,leftIn);
    CreateTree(t->right,rightPre,rightIn);
}
void Post(Tree t){
    if(t != nullptr){
        Post(t->left);
        P...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发