码迷,mamicode.com
首页 > 其他好文 > 详细

重建二叉树

时间:2015-05-06 21:10:23      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

题目:根据先序遍历和中序遍历重新还原出二叉树

 

思路:

1.找到先序第一个数字并赋值给根节点root->data;

2.在中序序列中找到先序的第一个数字;

3.中序左边的序列为根节点root的左子树(左递归),右边的序列为root的右子树(右递归)。

 

代码如下:

#include<iostream>
using namespace std;
struct Treenode
{
    int data;
    Treenode *lchild;
    Treenode *rchild;
};
//重建二叉树:递归构建
Treenode *build(int *first,int *second,int length)
{
    //判断参数是否合法
    if(first==NULL||second==NULL||length<=0)
        return NULL;
    int rootvalue=first[0];
    Treenode *root=new Treenode();
    root->data=rootvalue;
    root->lchild=root->rchild=NULL;
    //只有一个结点时返回
    if(length==1)
    {
        if(first[0]==second[0])
            return root;
        else
            throw exception("input error!");//抛出异常,C#中是throw new exception();
    }
    //多个结点时候递归
    else
    {
        int i=0;
        //在中序遍历中找到root结点
        while(i<length&&second[i]!=rootvalue)
            i++;
        if(i==length)
            throw exception("input error!!");
        //递归左子树,i左边的序列
        if(i>0)
        {
            root->lchild=build(first+1,second,i);
        }
        //递归右子树,i右边的序列
        if(i<length-1)
        {
            root->rchild=build(first+i+1,second+i+1,length-i-1);
        }
    }
    return root;
}
//按照中序打印二叉树
void print(Treenode *root)
{
    if(root)
    {
        print(root->lchild);
        cout<<root->data<<endl;
        print(root->rchild);
    }
}
int main()
{
    try
    {
        int a[10]={1,2,4,7,3,5,6,8};
        int b[10]={4,7,2,1,5,3,8,7};
        Treenode *root=build(a,b,8);
        print(root);
    }
    catch(const exception &e)
    {
        cerr<<e.what()<<endl;//cerr用来输出异常
    }
}

 

 

测试结果:

 

技术分享

重建二叉树

标签:

原文地址:http://www.cnblogs.com/runninglzw/p/4483042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!