我正在尝试删除所有的叶子。我知道叶子没有孩子,这就是我到目前为止所做的。
public void removeLeaves(BinaryTree n){
if (n.left == null && n.right == null){
n = null;
}
if (n.left != null)
removeLeaves(n.left);
if (n.right != null)
removeLeaves(n.right);
}
答案 0 :(得分:5)
n = null;
对您没有帮助,因为n
只是您函数的局部变量。相反,您需要在父级上设置n.left = null;
或n.right = null;
。
我不会给你一个完整的解决方案,因为这有点像家庭作业,但你可以,例如,你的函数添加一个返回值,以指示有问题的节点是否是一个叶子,并采取适当的措施父母的行动(在致电removeLeaves
之后)。
答案 1 :(得分:5)
如果你这样打破它会容易得多:
public void removeLeaves(BinaryTree n){
if (n.left != null) {
if (n.left.isLeaf()) {
n.removeLeftChild();
} else {
removeLeaves(n.left);
}
}
// repeat for right child
// ...
}
isLeaf
,removeLeftChild
和removeRightChild
应该无法实施。
答案 2 :(得分:3)
而不是n = null,它应该是:
if(n.parent != null)
{
if(n.parent.left == n)
{
n.parent.left = null;
}
else if(n.parent.right == n)
{
n.parent.right == null);
}
}
答案 3 :(得分:1)
由于Java通过值n = null;
传递引用,因此根本不起作用。有了这条线,n指向了叶子,现在什么都没有。所以你实际上并没有从父母那里删除它,你只是重新路由一个虚拟的本地引用。为解决方案做了马修所建议的。
答案 4 :(得分:1)
这是一个简单的 java 方法,可以从二叉树中删除叶子节点
public BinaryTreeNode removeLeafNode(BinaryTreeNode root) {
if (root == null)
return null;
else {
if (root.getLeft() == null && root.getRight() == null) { //if both left and right child are null
root = null; //delete it (by assigning null)
} else {
root.setLeft(removeLeafNode(root.getLeft())); //set new left node
root.setRight(removeLeafNode(root.getRight())); //set new right node
}
return root;
}
}
答案 5 :(得分:0)
/* @author abhineet*/
public class DeleteLeafNodes {
static class Node{
int data;
Node leftNode;
Node rightNode;
Node(int value){
this.data = value;
this.leftNode = null;
this.rightNode = null;
}
}
public static void main(String[] args) {
Node root = new Node(1);
Node lNode = new Node(2);
lNode.leftNode = new Node(4);
root.leftNode = lNode;
Node rNode = new Node(3);
rNode.rightNode = new Node(5);
root.rightNode = rNode;
printTree(root);
deleteAllLeafNodes(root, null,0);
System.out.println("After deleting leaf nodes::");
printTree(root);
}
public static void deleteAllLeafNodes(Node root, Node parent, int direction){
if(root != null && root.leftNode == null && root.rightNode == null){
if(direction == 0){
parent.leftNode = null;
}else{
parent.rightNode = null;
}
}
if(root != null && (root.leftNode != null || root.rightNode != null)){
deleteAllLeafNodes(root.leftNode, root, 0);
deleteAllLeafNodes(root.rightNode, root, 1);
}
}
public static void printTree(Node root){
if(root != null){
System.out.println(root.data);
printTree(root.leftNode);
printTree(root.rightNode);
}
}
}
答案 6 :(得分:0)
这应该有效 -
public boolean removeLeaves(Node n){
boolean isLeaf = false;
if (n.left == null && n.right == null){
return true;
//n = null;
}
if (n!=null && n.left != null){
isLeaf = removeLeaves(n.left);
if(isLeaf) n.left=null; //remove left leaf
}
if (n!=null && n.right != null){
isLeaf = removeLeaves(n.right);
if(b) n.right=null; //remove right leaf
}
return false;
}
答案 7 :(得分:0)
易于恢复的方法。
public static Node removeLeaves(Node root){
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return null;
}
root.left = removeLeaves(root.left);
root.right = removeLeaves(root.right);
return root;
}