我需要删除最小值的二叉搜索树,但我找不到一个干净的方式来做到这一点,目前我有一种干净的代码,虽然它doesn “T工作,我期望它的工作,我有这个代码(MTE MTE了左,右MTE和INT VAL键):设置参考对象密钥为空不能正常工作
MTE tempElement = root;
if(root == null) return;
else if((root.left == null) && (root.right == null))
{
root = null;
return;
}
else if(root.left != null)
{
while(tempElement.left != null) tempElement = tempElement.left;
if(tempElement.right == null) tempElement = null;
else tempElement = tempElement.right;
}
else if(root.right != null)
{
if(tempElement.left == null) root = tempElement;
else
{
while(tempElement.left != null) tempElement = tempElement.left;
root.val = tempElement.val;
if(tempElement.right == null) tempElement = null;
else tempElement = tempElement.right;
}
}
我这段代码有问题是,当我得到这个代码行 - if(tempElement.right == null) tempElement = null;
这是我提供的代码片段中的第13行。当我调试它时,它将tempElement更改为null,但我的主根元素不会更改其任何节点 - 我期望它的工作方式,任何解决方法?
您将本地变量tempElement设置为null,但不设置root的字段。很显然,root并没有改变。 – kaitoy