[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>> … Read more