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

Construct Binary Tree from Preorder and Inorder Traversal

时间:2014-11-24 11:26:30      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

提交成功的代码:

C++实现:

#include<iostream>
#include<new>
#include<vector>
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 *buildTree(vector<int> &preorder, vector<int> &inorder) {
        TreeNode *root=NULL;
        root=build(preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
        return root;
    }
    TreeNode *build(vector<int>::iterator pbegin,vector<int>::iterator pend,vector<int>::iterator ibegin,vector<int>::iterator iend)
    {
        TreeNode *root=NULL;
        if(pbegin==pend||ibegin==iend)
            return NULL;
        auto it=ibegin;
        while(it!=iend)
        {
            if(*it==*pbegin)
                break;
            it++;
        }
        root=new TreeNode(*pbegin);
        int len=it-ibegin;
        root->left=build(pbegin+1,pbegin+1+len,ibegin,it);
        root->right=build(pbegin+1+len,pend,it+1,iend);
        return root;
    }
    void preorder(TreeNode *root)
    {
        if(root)
        {
            cout<<root->val<<" ";
            preorder(root->left);
            preorder(root->right);
        }
    }
    void inorder(TreeNode *root)
    {
        if(root)
        {
            inorder(root->left);
            cout<<root->val<<" ";
            inorder(root->right);
        }
    }
    void postorder(TreeNode *root)
    {
        if(root)
        {
            postorder(root->left);
            postorder(root->right);
            cout<<root->val<<" ";
        }
    }
};

int main()
{
    vector<int> preorder={1,2,4,5,3,6,7};
    vector<int> inorder={4,2,5,1,6,3,7};
    Solution s;
    TreeNode *root=s.buildTree(preorder,inorder);
    s.preorder(root);
    cout<<endl;
    s.inorder(root);
    cout<<endl;
    s.postorder(root);
    cout<<endl;
}

运行结果:

bubuko.com,布布扣

 

出现:

Status: 

Memory Limit Exceeded

Submitted: 37 minutes ago
 
C++代码:
#include<iostream>
#include<new>
#include<vector>
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 *buildTree(vector<int> &preorder, vector<int> &inorder) {
        if(preorder.empty()||inorder.empty())
            return NULL;
        TreeNode *root=new TreeNode(preorder[0]);
        int i;
        for(i=0;i<(int)inorder.size();i++)
        {
            if(preorder[0]==inorder[i])
                break;
        }
        if(i>=(int)inorder.size())
            return NULL;
        vector<int> ileft(inorder.begin(),inorder.begin()+i);
        vector<int> iright(inorder.begin()+i+1,inorder.end());
        vector<int> pleft(preorder.begin()+1,preorder.begin()+1+i);
        vector<int> pright(preorder.begin()+1+i,preorder.end());
        root->left=buildTree(pleft,ileft);
        root->right=buildTree(pright,iright);
        return root;
    }
    void preorder(TreeNode *root)
    {
        if(root)
        {
            cout<<root->val<<" ";
            preorder(root->left);
            preorder(root->right);
        }
    }
    void inorder(TreeNode *root)
    {
        if(root)
        {
            inorder(root->left);
            cout<<root->val<<" ";
            inorder(root->right);
        }
    }
    void postorder(TreeNode *root)
    {
        if(root)
        {
            postorder(root->left);
            postorder(root->right);
            cout<<root->val<<" ";
        }
    }
};

int main()
{
    vector<int> preorder={1,2,4,5,3,6,7};
    vector<int> inorder={4,2,5,1,6,3,7};
    Solution s;
    TreeNode *root=s.buildTree(preorder,inorder);
    s.preorder(root);
    cout<<endl;
    s.inorder(root);
    cout<<endl;
    s.postorder(root);
    cout<<endl;
}

主要是重新分配了新的vector,出现内存超过限制。

 

Construct Binary Tree from Preorder and Inorder Traversal

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/wuchanming/p/4118040.html

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