我的代码是
TreeNode *sortedArrayToBST(vector<int> &num) {
function<TreeNode*(int,int)> func=
[&func,&num](int s, int e){
TreeNode* p = NULL;
if(s>e) return NULL; // change to return p would compile
int m = (s+e)/2;
p = new TreeNode(num[m]);
p->left = func(s,m-1);
p->right = func(m+1,e);
return p;
};
return func(0,num.size()-1);
}
Solutions.cpp:957:21: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
Solutions.cpp:959:29: error: inconsistent types ‘TreeNode*’ and ‘int’ deduced for lambda return type
Solutions.cpp:959:29: error: invalid conversion from ‘TreeNode*’ to ‘int’ [-fpermissive]
Solutions.cpp:962:12: error: inconsistent types ‘TreeNode*’ and ‘int’ deduced for lambda return type
Solutions.cpp:962:12: error: invalid conversion from ‘TreeNode*’ to ‘int’ [-fpermissive]
我通过创建 TreeNode* 类型的 NULL 来修复代码。我的问题是如何创建一个类型为 NULL,这样我就不需要声明一个临时变量来返回 NULL 指针。像 NULL(TreeNode);