网站推广建设加盟,茶叶网站建设目的,漳州网站开发点博大a优,启动门户网站建设在leetcode里面刷题出现的问题#xff0c;当我在sortedArrayToBST里面给root赋予初始值NULL之后#xff0c;问题得到解决#xff01; 理论上root是未初始化的变量#xff0c;然后我进入insert函数之后#xff0c;root引用的内容也是未知值#xff0c;因此无法给原来的二叉…在leetcode里面刷题出现的问题当我在sortedArrayToBST里面给root赋予初始值NULL之后问题得到解决 理论上root是未初始化的变量然后我进入insert函数之后root引用的内容也是未知值因此无法给原来的二叉树完成初始化 本题解决方案要么是给root赋予NULL初始值要么是去掉if(!root)这一行 class Solution {
public:TreeNode* sortedArrayToBST(vectorint nums) {TreeNode* root;int sznums.size();insert(nums,root,0,sz-1);return root;}void insert(vectorint v,TreeNode* root, int l,int r){if(lr){int mid(lr)/2;if(!root)rootnew TreeNode(v[mid]);insert(v,root-left,l,mid-1);insert(v,root-right,mid1,r);}}};