C++ C++;递归中的结构返回nullptr?;

C++ C++;递归中的结构返回nullptr?;,c++,struct,C++,Struct,我正在解决这个问题。我知道如何在这个问题中使用递归。但我对结构的最小化感到困惑。通过的版本如下: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class

我正在解决这个问题。我知道如何在这个问题中使用递归。但我对结构的最小化感到困惑。通过的版本如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int len = inorder.size();
        if (inorder.empty() && postorder.empty())
            return nullptr;
        return buildTree(inorder, 0, len - 1, postorder, 0, len - 1);
    }

    int getIndex (vector<int> order, int key)
    {
        int i = 0;
        for(int n: order)
        {
            if (n == key)
                break;
            i++;
        }
        return i;
    }

    TreeNode* buildTree(vector<int>& inorder, int is, int ie, vector<int>& postorder, int ps, int pe)
    {
        if (is > ie || ps > pe)
            return nullptr;
        TreeNode* root = new TreeNode(postorder[pe]);
        int index = getIndex(inorder, postorder[pe]);
        //cout<< index <<" "<<postorder[pe]<< endl;
        root->left = buildTree(inorder, is, index - 1, postorder, ps, ps + index - is - 1);
        root->right = buildTree(inorder, index + 1, ie, postorder, pe - ie + index,pe - 1);
        return root;
    }
};
TreeNode* buildTree(vector<int>& inorder, int is, int ie, vector<int>& postorder, int ps, int pe)
{
    if (is > ie || ps > pe)
        return nullptr;
    struct TreeNode root(postorder[pe]);
    int index = getIndex(inorder, postorder[pe]);
    //cout<< index <<" "<<postorder[pe]<< endl;
    root.left = buildTree(inorder, is, index - 1, postorder, ps, ps + index - is - 1);
    root.right = buildTree(inorder, index + 1, ie, postorder, pe - ie + index,pe - 1);
    //cout<< root.val << " " << root.right << " " << root.left <<endl;
    return &root;
}
/**
*二叉树节点的定义。
*树状结构{
*int-val;
*TreeNode*左;
*TreeNode*对;
*TreeNode(intx):val(x),左(NULL),右(NULL){
* };
*/
类解决方案{
公众:
TreeNode*buildTree(向量和索引、向量和后序){
int len=inorder.size();
if(inoorder.empty()&&postorder.empty())
返回空ptr;
返回buildTree(顺序为0,len-1,后序为0,len-1);
}
int getIndex(向量顺序,int键)
{
int i=0;
for(整数n:订单)
{
如果(n==键)
打破
i++;
}
返回i;
}
TreeNode*buildTree(向量和索引、int-is、int-ie、向量和后序、int-ps、int-pe)
{
如果(is>ie | ps>pe)
返回空ptr;
TreeNode*root=新的TreeNode(postorder[pe]);
int index=getIndex(按顺序,按顺序[pe]);

//coutnew
操作符将动态分配存储在堆上的内存。不起作用的第二个版本只是创建变量并将其存储在堆栈上。在该函数调用完成后,所有信息都将从堆栈中弹出,并且存储在这些内存位置的任何内容都将不可用不再被认为是有效的。例如,在该函数完成后,说
return&root
返回一个无效的地址。

您所做的区别是您创建了一个。简而言之,您不应该返回地址(通过引用)因为局部变量在超出范围时会被释放。相反,您应该返回
根的副本(按值)。因此,不需要
&
,但如果树数据不是基元类型(不是
int
double
,…等),请确保您有一个副本构造函数

您需要启用编译器警告。
return&root;
正在返回局部变量的地址。您不能这样做,因为函数结束时它超出了范围。好的,现在我知道新关键字malloc在递归过程中是一个持久内存,而局部结构变量被破坏。非常感谢!@liuqi“现在我知道,新的关键字Malc”在C++中不使用<代码> MalOC ,因为它不调用构造函数。