摸森堡 2020-04-10 17:24 采纳率: 0%
浏览 340
已采纳

为什么我使用结构体会出错?

我在写通过二叉树前序和中序遍历的结果重构二叉树,但是TreeNode结构体一直报错,不知道哪里出了问题

这是编译器提示的错误信息

图片说明

这是代码

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stdlib.h>

using namespace std;

TreeNode *reConstructBinaryTree(vector<int> pre,vector<int> vin);
TreeNode *buildtreehelper(unordered_map <int,int> &m,vector<int> &pre,int s0,int e0,int s1);

 struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode (int x) : val(x), left(NULL), right(NULL) {}
  };

int _tmain(int argc, _TCHAR* argv[])
{
    int a[]={1,2,4,5,3,6};
    int b[]={4,2,5,1,3,6};
    vector<int> c(a,a+6);
    vector<int> d(b,b+6);
    unordered_map<int ,int> in;
    unordered_map <int,int> :: iterator p;
    for(int i=0;i<d.size();++i)
    {
       in[d[i]]=i;
    }
    TreeNode *f=reConstructBinaryTree (c,d);
    cout<<f->val <<endl;
    cout<<f->left ->val <<endl;
    cout<<f->right ->val <<endl;
    return 0;
}

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
    unordered_map<int ,int> in;
    unordered_map <int,int> :: iterator p;
    if(pre.empty ())
        return nullptr;
    for(int i=0;i<vin.size();++i)
    {
       in[vin[i]]=i;
    }

    return buildtreehelper (in,pre,0,pre.size()-1,0);
    }

 TreeNode *buildtreehelper(unordered_map <int,int> &m,vector<int> &pre,int s0,int e0,int s1)
{
   if(s0>e0)
       return nullptr ;
   int mid=pre[s1];
   int index=m[mid];
   int leftlen=index-s0-1;
   TreeNode *node=new TreeNode (mid);
   node->left=buildtreehelper (m,pre,s0,index-1,s1+1);
   node->right =buildtreehelper(m,pre,index-1,e0,s0+leftlen+2);
   return node;
}
  • 写回答

2条回答 默认 最新

  • zx289544730 2020-04-12 17:13
    关注

    struct TreeNode要放在

    TreeNode *reConstructBinaryTree(vector<int> pre,vector<int> vin);
    TreeNode *buildtreehelper(unordered_map <int,int> &m,vector<int> &pre,int s0,int e0,int s1);
    

    上面啊,兄嘚。

    改成

    struct TreeNode {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode (int x) : val(x), left(NULL), right(NULL) {}
    };
    
    TreeNode *reConstructBinaryTree(vector<int> pre,vector<int> vin);
    TreeNode *buildtreehelper(unordered_map <int,int> &m,vector<int> &pre,int s0,int e0,int s1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题