展开全部
树的遍历多用递归,从根节点62616964757a686964616fe78988e69d8331333335316533出发,对子数进行逐级迭代/**
* 以p为根向下访问x层
* @param layer 存储结果
*/
public void layerX(List layer, Node p, int x) {
if (p != null) {
// 至访问层的节点
if (x == 1) {
layer.add(p);
}
// 继续 递归访问以字节点为(参照)根访问x-1层
Node[] c = p.getChildren();
if (c != null) {
for (Node n : c) {
layerX(layer, n, x - 1);
}
}
}
}
class Node {
private String name; // 节点名称
private Node[] children; // 子节点
public Node(String name, Node[] children) {
this.name = name;
this.children = children;
}
//getter,setter
}