如何删除二叉树的叶子?

发布于 2024-08-29 00:11:16 字数 310 浏览 8 评论 0原文

我正试图把所有的叶子都去掉。我知道叶子没有孩子,这就是我到目前为止所拥有的。

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

}

I'm trying to remove all of the leaves. I know that leaves have no children, this is what I have so far.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流绪微梦 2024-09-05 00:11:16

n = null; 对您没有帮助,因为 n 只是函数的局部变量。相反,您需要在父级上设置 n.left = null;n.right = null;

我不会给你一个完整的解决方案,因为这听起来很像家庭作业,但你可以,例如,向你的函数添加一个返回值,以指示所讨论的节点是否是叶子,并在父级(在调用 removeLeaves 之后)。

n = null; won't help you, since n is just a local variable of your function. Instead, you'd need to set n.left = null; or n.right = null; on the parent.

I won't give you a complete solution, since this smells a lot like homework, but you could, for example, add a return value to your function to indicate whether the node in question is a leaf or not and take appropriate actions in the parent (after the call to removeLeaves).

悲喜皆因你 2024-09-05 00:11:16

如果你像这样分解它就会容易得多:

public void removeLeaves(BinaryTree n){
  if (n.left != null) {
    if (n.left.isLeaf()) {
      n.removeLeftChild();
    } else {
      removeLeaves(n.left);
    }
  }
  // repeat for right child
  // ...
}

isLeafremoveLeftChildremoveRightChild应该很容易实现。

It's much easier if you break this down like this:

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 and removeRightChild should be trivial to implement.

完美的未来在梦里 2024-09-05 00:11:16

而不是 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);
    }
  }

Instead of n = null, it should be:

if(n.parent != null)
  {
    if(n.parent.left == n)
    {
      n.parent.left = null;
    } 
    else if(n.parent.right == n)
    {
      n.parent.right == null);
    }
  }
眼眸 2024-09-05 00:11:16

由于 Java 通过值传递引用,n = null; 根本不起作用。这条线 n 原本指向叶子,现在指向任何东西。所以你实际上并没有从父级中删除它,你只是重新路由一个虚拟的本地引用。对于解决方案,请按照马修的建议进行操作。

Since Java passes references by values n = null; simply does not work. With this line n was pointing to the leaf and now points to nothing. So you aren't actually removing it from the parent, you are just rerouting a dummy local reference. For the solution do what Matthew suggested.

伴我心暖 2024-09-05 00:11:16

这是一个简单的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;
    }

}

Here's a simple java method to delete leaf nodes from binary tree

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

}
人疚 2024-09-05 00:11:16

简单的方法与recussion。

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

Easy method with recusrion .

 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;
  }
夜光 2024-09-05 00:11:16
 /* @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);
        }
    }

}
 /* @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);
        }
    }

}
好久不见√ 2024-09-05 00:11:16

这应该有效-

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;

}

This should work-

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文