[Solved] TreeNode cannot be cast to java.lang.Comparable?


Your problem is that while the generic parameter of TreeNode is Comparable, the class itself is not.

Change this:

public static class TreeNode<E extends Comparable<E>> {

To this:

public static class TreeNode<E extends Comparable<E>> implements Comparable<TreeNode<E>> {

Or if you don’t require the generic type to also be Comparable:

public static class TreeNode<E> implements Comparable<TreeNode<E>> {

Once you make this change, you won’t need the cast.

5

solved TreeNode cannot be cast to java.lang.Comparable?