算法--二叉树

1、根据前序遍历和中序遍历构造二叉树

#include <iostream>

using namespace std;
typedef struct BiNode {
    char data;
    struct BiNode *lchild;
    struct BiNode *rchild;
}BiNode, *BiTree;

void CreateBiTree(BiTree &t, string presequence, string insequence) {
    if(presequence.length()==0) {
       t=NULL;
        return;
    }
    char rootNode = presequence[0];//根
    int index = insequence.find(rootNode);//根在中序序列中的位置
    string lchild_insequence=insequence.substr(0, index);//左孩子的中序序列
    string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列
    int lchild_length = lchild_insequence.length();
    int rchild_length = rchild_insequence.length();
    string lchild_presequence=presequence.substr(1, lchild_length);//左孩子的前序序列
    string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列

    t = new BiNode;
    if(t != NULL) {
        t->data=rootNode;
        CreateBiTree(t->lchild, lchild_presequence, lchild_insequence);
        CreateBiTree(t->rchild, rchild_presequence, rchild_insequence);
    }
}

void PreOrderTraverse(BiTree &t) {
    if( t!= NULL ) {
        cout << t->data;
        PreOrderTraverse(t->lchild);
        PreOrderTraverse(t->rchild);
    }
}

void InOrderTraverse(BiTree &t) {
    if( t!= NULL ) {
        InOrderTraverse(t->lchild);
        cout << t->data;
        InOrderTraverse(t->rchild);
    }
}
int main() {
    BiTree t;
    string presequence="ABCDEFG";
    string insequence="CBEDAFG";
    CreateBiTree(t, presequence, insequence);

    PreOrderTraverse(t);
    cout << endl;
    InOrderTraverse(t);


    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Definition for binary tree
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        if(pre.empty())
            return NULL;
        int root = pre.at(0);//根节点的值
        TreeNode *t = new TreeNode(root);//根节点

        int index = 0;//根节点在中序遍历中的位置
        vector<int>::iterator iElement = find(vin.begin(), vin.end(), root);
        if(iElement != vin.end()) {
            index  = distance(pre.begin(), iElement);
        }

        vector<int> lVinSequence;//左中序序列
        vector<int> rVinSequence;//右中序序列
        lVinSequence.assign(vin.begin(),vin.begin()+index);
        rVinSequence.assign(vin.begin()+index+1,vin.end());
        int lLength = lVinSequence.size();//左先序序列长度
        int rLength = rVinSequence.size();//右先序序列长度
        vector<int> lPreSequence;//左先序序列
        vector<int> rPreSequence;//右先序序列
        lPreSequence.assign(pre.begin()+1,pre.begin()+lLength+1);
        rPreSequence.assign(pre.begin()+lLength+2,pre.end());

        t->left = reConstructBinaryTree(lPreSequence, lVinSequence);
        t->right = reConstructBinaryTree(lPreSequence, rVinSequence);

        return t;
    }
};

void showVector(vector<int> v) {
    for(vector<int>::iterator iter=v.begin(); iter != v.end(); ++iter) {
        cout << *iter << " ";
    }
    cout << endl;
}

void showPreSequence(TreeNode *t) {
    if( t!= NULL ) {
        cout << t->val << " ";
        showPreSequence(t->left);
        showPreSequence(t->right);
    }
}

void showInSequence(TreeNode *t) {
    if( t!= NULL ) {
        showPreSequence(t->left);
        cout << t->val << " ";
        showPreSequence(t->right);
    }
}
int main() {
    vector<int> pre;
    vector<int> vin;
    //前序遍历序列{1,2,4,7,3,5,6,8}
    pre.push_back(1);pre.push_back(2);pre.push_back(4);pre.push_back(7);pre.push_back(3);pre.push_back(5);pre.push_back(6);pre.push_back(8);
    //中序遍历序列{4,7,2,1,5,3,8,6}
    vin.push_back(4);vin.push_back(7);vin.push_back(2);vin.push_back(1);vin.push_back(5);vin.push_back(3);vin.push_back(8);vin.push_back(6);
    cout << "先序遍历: " ; showVector(pre);
    cout << "中序遍历: " ; showVector(vin);
// vector<int>::iterator iElement = find(pre.begin(), pre.end(), 7);
// int nPosition;
// if(iElement != pre.end()) {
// nPosition = distance(pre.begin(), iElement);
// cout << "Value :" << *iElement << " ";
// cout << "find in the Vector at position :" << nPosition + 1 << endl;
// }

// vector<int> ltmp;
// vector<int> rtmp;
// ltmp.assign(pre.begin(), pre.begin()+nPosition);
// rtmp.assign(pre.begin()+nPosition+1, pre.end());
// showVector(ltmp);
// showVector(rtmp);
//
// ltmp.assign(pre.begin()+1, pre.begin()+4);
// rtmp.assign(pre.begin()+5, pre.end());
// showVector(ltmp);
// showVector(rtmp);

    Solution s;
    TreeNode *t  = s.reConstructBinaryTree(pre, vin);
    showInSequence(t);
    return 0;
}
    原文作者:扎手的苍耳
    原文地址: https://blog.csdn.net/ZWX2445205419/article/details/73606057
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞