以下是问题链接。https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
class Solution {
public:
void dfs(TreeNode *&p,int l,int r,vector<int> &num){
if(l>r){p=NULL; return;}
p->val=num[(l+r)/2];
TreeNode *left,*right;
p->left=left;
p->right=right;
dfs(left,l,(l+r)/2-1,num);
dfs(right,(l+r)/2+1,r,num);
}
TreeNode *sortedArrayToBST(vector<int> &num) {
TreeNode *head;
dfs(head,0,num.size()-1,num);
return head;
}
};
上面的代码出现运行时错误,但我找不到原因。你能帮帮我吗?