如何获取树的所有元素?
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
函数。
关于java - 使用 Java 逐层构建树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313938/