Given what you have bst, you can do a walk around inside, this will give you all the elements in ascending order (sorted), an example of how this is done:
public List<Integer> toList() {
return createOrderedList(this);
}
private List<Integer> createOrderedList(Node root) {
if(root == null) {
return new ArrayList<>();
}
List<Integer> list = new ArrayList<>();
list.addAll(createOrderedList(root.left));
list.add(root.data);
list.addAll(createOrderedList(root.right));
return list;
}
source
share