前序+中序 /前序+后序/中序+后序重建二叉树 C++

标准版:

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

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

vector<int> sub(vector<int> v, int op, int ed)  //截取子树
{
    vector<int> ans;
    for(int i=op; i<=ed; i++) ans.push_back(v[i]);
    return ans;
}
int find_index(vector<int> v, int root)  //找寻根位置
{
    for(int i=0;i<int(v.size());i++) if(v[i]==root) return i;

}

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) //前序+中序重建二叉树
{
        if(pre.empty()) return NULL;
        int root = pre[0];
        TreeNode *tree = new TreeNode(root);
        int root_index = find_index(vin,root);
        
        vector<int> left_pre,left_vin,right_pre,right_vin;
        
        left_pre = sub(pre,1,root_index);
        left_vin = sub(vin,0,root_index-1);
        
        right_pre = sub(pre,root_index+1, pre.size()-1);
        right_vin = sub(vin,root_index+1,vin.size()-1);
        
        tree->left = reConstructBinaryTree(left_pre,left_vin);
        tree->right = reConstructBinaryTree(right_pre,right_vin);
        
        return tree;
}

 

 

 

 

其他版:

#include<iostream>
using namespace std;

template <typename T>
struct BinaryNode{
  T elem;
  BinaryNode *left;
  BinaryNode * right;
  BinaryNode(T d, BinaryNode *l=NULL, BinaryNode *r=NULL):elem(d),left(l),right(r){};
};



//前序+中序
BinaryNode<char>* rebuildTree1(const string &preorder, const string &inorder)
{
    if(preorder.empty())return NULL;
    char root=preorder[0];

    BinaryNode<char> *tree=new BinaryNode<char>(root);

    size_t index=inorder.find(root);
    string left_preorder,left_inorder,right_preorder,right_inorder;

    left_preorder=preorder.substr(1,index);
    left_inorder=inorder.substr(0,index);

    right_preorder=preorder.substr(index+1,preorder.size()-1);
    right_inorder=inorder.substr(index+1,inorder.size()-1);

    tree->left=rebuildTree1(left_preorder,left_inorder);
    tree->right=rebuildTree1(right_preorder,right_inorder);

    return tree;
}

//中序+后序
BinaryNode<char>* rebuildTree2(string &inorder , string &postorder)
{
    if(postorder.empty()||inorder.empty())return NULL;
    size_t last=postorder.size()-1;
    char root=postorder[last];
    cout<<"root:"<<root<<endl;

    BinaryNode<char> *tree=new BinaryNode<char>(root);

    size_t index=inorder.find(root);

    string left_inorder,right_inorder,left_postorder,right_postorder;

    left_inorder=inorder.substr(0,index);
    right_inorder=inorder.substr(index+1,inorder.size()-1);

    left_postorder=postorder.substr(0,index);
    right_postorder=postorder.substr(index,postorder.size()-1-index);

    tree->left=rebuildTree2(left_inorder,left_postorder);
    tree->right=rebuildTree2(right_inorder,right_postorder);

    return tree;

}
//前序+后序
BinaryNode<char>* rebuildTree3(string &preorder , string &posorder)
{
    if(preorder.empty()||posorder.empty())return NULL;
    char root=preorder[0];
    BinaryNode<char>* tree=new BinaryNode<char>(root);

    char lastpre=preorder[preorder.size()-1];
    size_t index=posorder.find(lastpre);
    string left_preorder,right_preorder,left_posorder,right_posorder;

    left_preorder=preorder.substr(1,index);
    right_preorder=preorder.substr(index+1,preorder.size()-1);

    left_posorder=posorder.substr(0,index);
    right_posorder=posorder.substr(index,posorder.size()-1-index);

    tree->left=rebuildTree3(left_preorder,left_posorder);
    tree->right=rebuildTree3(right_preorder,right_posorder);

    return tree;
}

int main()
{
    string prestr,instr,posstr;
    cin>>prestr>>instr>>posstr;

    BinaryNode<char> *root3=rebuildTree3(prestr,posstr);
    cout<<"pre_pos:"<<endl;
    cout<<root3->left->elem<<endl;

    cout<<root3->left->left->elem<<endl;
    cout<<root3->left->right->elem<<endl;

}
/*
138245
832145
823541
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值