我尝试使用removeMin()
方法按排序顺序打印出我的二叉搜索树,但输出不正确。
这是我的代码:
public Node removeMin(Node insertNode){
Node parentNode =root;
if (insertNode.left != null){
return removeMin(insertNode.left);
}
if (insertNode.right ==null){
parentNode.left = null;
}else {
parentNode.left = removeMin(insertNode.right);
}
return insertNode;
}
答案 0 :(得分:0)
检查node == NULL
public Node removeMin(Node root){
if(root == NULL)
return NULL;
if (root.left != NULL){
return removeMin(root.left);
}
if (insertNode.right == NULL){
Node temp = root;
root = NULL;
return temp;
}
else {
return removeMin(root.right);
}
}
未选中..
答案 1 :(得分:0)
试试这个
private Node removeMin(Node node){
if(node.left.left == null) {
Node minNode = node.left;
node.left = node.left.right;
minNode.right = null;
return minNode;
} else {
return removeMin(node.left);
}
}
public Node removeMin() {
if(root == null) {
return null;
} else if(root.left != null) {
return removeMin(root);
} else {
Node minNode = root;
root = root.right;
minNode.right = null;
return minNode;
}
}