Как я могу получить все элементы дерева?
public static <E> PositionList<E> width(Tree<E> t){
//I've initialized a queue and a PositionList<E>
Queue<E> aux = new NodeQueue<E>();
PositionList<E> positionAux = new NodePositionList<E>();
Position<E> pointer = t.root();
...
но тогда я не знаю, как продолжить...
Пример:
1
| |
2 3
| | | |
4 5 6 7
Верните список [1,2,3, 4, 5, 6, 7]
Используйте очередь, чтобы выполнить траверс уровня. Вы можете увидеть мою реализацию: https://github.com/shaogbi/Java/blob/master/datastructure/MyBinaryTree.java см. levelTraverse
.