I'm trying to print the path from the root to a specified node. But the issue here that I have to use the node in the parameter, as: public ArrayList> pathOf(TreeNode x)
I've tried this:
public java.util.ArrayList<TreeNode<E>> pathOf(TreeNode<E> x) {
ArrayList<TreeNode<E>> list = new ArrayList<TreeNode<E>>();
TreeNode<E> current = root; // Start from the root
while (current != null) {
list.add(current); // Add the node to the list
if (x.element.compareTo(current.element) < 0) {
current = current.left;
} else if (x.element.compareTo(current.element) > 0) {
current = current.right;
} else {
break;
}
}
return list; // Return an array of nodes
}
When I'm calling the method: System.out.println("path of : " + tree.pathOf(66));
I'm getting this message: incompatible types: int cannot be converted to TreeNode
Thank you in advance