BUG未解之谜01-指针引用之谜

在leetcode里面刷题出现的问题,当我在sortedArrayToBST里面给root赋予初始值NULL之后,问题得到解决!

理论上root是未初始化的变量,然后我进入insert函数之后,root引用的内容也是未知值,因此无法给原来的二叉树完成初始化!

本题解决方案要么是给root赋予NULL初始值,要么是去掉if(!root)这一行!

cpp 复制代码
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        TreeNode* root;
        int sz=nums.size();
        insert(nums,root,0,sz-1);
        return root;
    }
    void insert(vector<int>& v,TreeNode*& root, int l,int r){
        if(l<=r){
            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);
        }
    }

};
相关推荐
nnsix10 小时前
Unity ReferenceFinder插件 多选资源查找bug解决
unity·游戏引擎·bug
中冕—霍格沃兹软件开发测试14 小时前
边界值分析:功能测试中的精度利器
人工智能·功能测试·科技·测试工具·appium·bug
中冕—霍格沃兹软件开发测试1 天前
探索性测试:思维驱动下的高效缺陷狩猎
人工智能·科技·开源·appium·bug
中冕—霍格沃兹软件开发测试2 天前
Git版本控制在测试项目管理中的应用
人工智能·git·科技·开源·appium·bug
中冕—霍格沃兹软件开发测试2 天前
用户体验测试:功能与界面并重
人工智能·科技·开源·appium·bug·ux
中冕—霍格沃兹软件开发测试3 天前
测试工具链的构建与团队协作:从工具集成到价值流动
人工智能·科技·测试工具·开源·appium·bug
yuxuan66993 天前
【Docker】使用docker启动禅道出现mysql.sock 文件已经存在的bug
mysql·docker·centos·bug
zfxwasaboy3 天前
BUG: failure at drivers/pci/msi.c:376/free_msi_irqs()!
linux·c语言·bug
yscript3 天前
GPU分配BUG: Duplicate GPU detected : rank 1 and rank 0 both on CUDA device d5000
linux·运维·服务器·vscode·bug
xiucai_cs3 天前
【后端】开发过程中如何尽可能的减少 bug 的产生
后端·bug