• Construct Binary Tree from Preorder and Inorder Traversal


    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;
    }

    运行结果:

    出现:

    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,出现内存超过限制。

  • 相关阅读:
    springboot与docker
    Docker入门笔记(Centos7)
    记录VUE-CLI项目创建及初始化相关
    centos下安装mysql5.6
    GitLab权限介绍
    属性文件操作之Properties与ResourceBundle
    Shell入门基础
    JavaScript基础的记录
    Java基本排序算法
    解读闭包,这次从ECMAScript词法环境,执行上下文说起
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4118040.html
Copyright © 2020-2023  润新知