在leetCode里面刷题出现的问题,当我在sortedArrayToBST里面给root赋予初始值NULL之后,问题得到解决!
理论上root是未初始化的变量,然后我进入iNSErt函数之后,root引用的内容也是未知值,因此无法给原来的二叉树完成初始化!
本题解决方案要么是给root赋予NULL初始值,要么是去掉if(!root)这一行!
class Solution { public: TreeNode* sortedArrayToBST(vector& nums) { TreeNode* root; int sz=nums.size(); insert(nums,root,0,sz-1); return root; } void insert(vector& v,TreeNode*& root, int l,int r){ if(l int mid=(l+r)/2; if(!root) root=new TreeNode(v[mid]); insert(v,root-left,l,mid-1); insert(v,root->right,mid+1,r); } } };