How do I remove the leaves of a binary tree?

Posted by flopex on Stack Overflow See other posts from Stack Overflow or by flopex
Published on 2010-04-14T06:28:22Z Indexed on 2010/04/14 6:33 UTC
Read the original article Hit count: 244

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

}

© Stack Overflow or respective owner

Related posts about java

Related posts about algorithm