文章

17

粉丝

0

获赞

92

访问

2.2k

头像
二叉树遍历2 题解:
P1401 华中科技大学/中国科学院机试题
发布于2026年3月16日 14:07
阅读数 45

学的是代码随想录的方法,代码中包括如何打印切割后的左右子树

#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
    char val;
    struct TreeNode *left,*right;
    TreeNode(char c):val(c),left(NULL),right(NULL){}
};
TreeNode* build(const string& pre,const string& in){
    if(pre.size()==0) return NULL;
    char rootValue = pre[0];
    TreeNode* root = new TreeNode(rootValue);
    // 寻找切割点
    int index = 0;
    for(index=0;index<in.size();++index){
        if(in[index]==rootValue){
            break;
        }
    } 
    //切割中序数组
    string leftin(in.begin(),in.begin()+index);
//    cout<<"leftin"<<leftin<<"\n";
  ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发