剑指Offer系列-面试题6:重建二叉树

题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建出二叉树并输出它的头结点。

代码没有重新写,是15年秋数据结构的OJ题,徒手写的队列和二叉树的类。

#include 

using namespace std;
/***********************************Queue******************************/
template
struct LinkNode
{
    T data;
    LinkNode *next;
    LinkNode(LinkNode *top = NULL)
    {
        next = top;
    }
    LinkNode(T elem,LinkNode *top = NULL)
    {
        data = elem;
        next = top;
    }
};

template
class Queue
{
private:
    LinkNode *first,*rear;
public:
    Queue()
    {
        rear = first = new LinkNode;
    }

    ~Queue()
    {
        makeEmpty();
    }

    bool EnQueue(const T& elem)
    {
        LinkNode* newNode = new LinkNode(elem);
        if(!newNode)
            return false;
        if(IsEmpty())
            first->next = rear = newNode;
        rear->next=newNode;
        rear=rear->next;
        return true;
    }

    bool DeQueue(T& elem)
    {
        LinkNode* del;
        if(first==rear)
            return false;
        del=first->next;
        elem=del->data;
        first->next=del->next;
        if(first->next==NULL)
            rear=first;
        delete del;
        return true;
    }

    bool IsEmpty()
    {
        return (first == rear)? true:false;
    }

    void makeEmpty()
    {
        LinkNode *p = first->next;
        if(!p)
            return;
        while(first->next)
        {
            first = p->next;
            delete p;
            p = first->next;
        }
    }

};
/*********************************BinaryTree***************************/
template
struct BinTreeNode
{
    T data;
    BinTreeNode *lchild, *rchild;
    BinTreeNode()
    {
        lchild = NULL;
        rchild = NULL;
    }
    BinTreeNode(T x, BinTreeNode* l = NULL, BinTreeNode* r = NULL)
    {
        data = x;
        lchild = l;
        rchild = r;
    }
};

template
class BinaryTree
{
private:
    BinTreeNode* root;
    void destroy(BinTreeNode* root)
    {
        if(root)
        {
            destroy(root->lchild);
            destroy(root->rchild);
            delete root;
        }
    }
public:
    BinaryTree()
    {
        root = NULL;
    }
    BinaryTree(BinTreeNode* p)
    {
        root = p;
    }
    BinaryTree(T *elems, int n)         ///构造函数,层序输入,建立满二叉树
    {
        root = new BinTreeNode [n];
        for(int i=0; i& a);
    ~BinaryTree()
    {
    }
    BinTreeNode *visitRoot()
    {
        return root;
    }
    bool isEmpty()
    {
        return (root == NULL)?true:false;
    }
    BinTreeNode *Parent(BinTreeNode* current)
    {
        return (root == NULL || root == current)?NULL:Parent(root,current);
    }
    BinTreeNode *LeftChild(BinTreeNode* current)
    {
        return (current != NULL)?current->lchild:NULL;
    }
    BinTreeNode *RightChild(BinTreeNode* current)
    {
        return (current != NULL)?current->rightChild:NULL;
    }
    void PreOrder(BinTreeNode* p = visitRoot())   ///递归前序遍历
    {
        if(p)
        {
            cout << p->data << " ";
            PreOrder(p->lchild);
            PreOrder(p->rchild);
        }
    }
    void InOrder(BinTreeNode* p = visitRoot())   ///递归中序遍历
    {
        if(p)
        {
            InOrder(p->lchild);
            cout << p->data << " ";
            InOrder(p->rchild);
        }
    }
    void PostOrder(BinTreeNode* p = visitRoot())   ///递归后序遍历
    {
        if(p)
        {
            PostOrder(p->lchild);
            PostOrder(p->rchild);
            cout << p->data << " ";
        }
    }
    void LevelOrder(BinTreeNode* p) ///非递归层序遍历
    {
        Queue*> q;
        BinTreeNode* pre = root;
        q.EnQueue(pre);
        cout << pre->data << " ";
        while(!q.IsEmpty())
        {
            q.DeQueue(pre);
            if(pre->lchild != NULL)
            {
                q.EnQueue(pre->lchild);
                cout << pre->lchild->data << " ";
            }
            if(pre->rchild != NULL)
            {
                q.EnQueue(pre->rchild);
                cout << pre->rchild->data << " ";
            }
        }
    }


};

template 
BinTreeNode *CreateBinTree(T* VLR, T* LVR, int n)   ///根据前序和中序递归重建二叉树
{
    if(n == 0)
        return NULL;
    int k = 0;
    while(VLR[0] != LVR[k])
        k++;
    BinTreeNode *t = new BinTreeNode(VLR[0]);
    t->lchild = CreateBinTree(VLR+1,LVR,k);
    t->rchild = CreateBinTree(VLR+k+1,LVR+k+1,n-k-1);
    return t;
}

int main()
{
    int n = 8;
    int a[8] = {1,2,4,7,3,5,6,8};   ///前序序列
    int b[8] = {4,7,2,1,5,3,8,6};   ///中序序列

    BinTreeNode *p = CreateBinTree(a,b,n);
    BinaryTree tree(p);
    tree.LevelOrder(p);     ///层序输出
    cout << endl;
    tree.PostOrder(p);      ///后序输出

    return 0;
}


你可能感兴趣的:(剑指Offer,剑指Offer)