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);
        }
    }

};
相关推荐
冬奇Lab4 小时前
稳定性性能系列之四——异常日志机制与进程冻结:问题排查的黑匣子
android·性能优化·车载系统·bug
_OP_CHEN5 小时前
【测试理论与实践】(三)测试BUG篇:从 BUG 本质到实战博弈,带你吃透软件测试的核心逻辑
运维·测试开发·产品运营·bug·压力测试·测试
-拟墨画扇-2 天前
Git | Bug分支操作
git·gitee·github·bug·gitcode
小凡子空白在线学习2 天前
Bug目录
bug
jiedaodezhuti3 天前
秒级定位线上Bug的一些命令
bug
l1t5 天前
修改一个触发PostgreSQL 17.2 bug的SQL
sql·postgresql·bug
包小黑5 天前
【Linux】bug登记好习惯:发现bug,用命令行截取对应日志
linux·bug
癫狂的兔子7 天前
【BUG】【Python】逆序取值为空
bug
癫狂的兔子7 天前
【BUG】【Python】精确度问题
python·bug
癫狂的兔子7 天前
【BUG】【Python】合并两个列表
bug