depthNode method

List<T> depthNode(
  1. int depth, [
  2. bool only = true
])

从当前节点开始查找深度等于给定深度的节点 广度优先遍历 only==true 只返回对应层次的,否则返回<=

Implementation

List<T> depthNode(int depth, [bool only = true]) {
  if (deep > depth) {
    return [];
  }
  List<T> resultList = [];
  List<T> tmp = [this as T];
  List<T> next = [];
  while (tmp.isNotEmpty) {
    for (var node in tmp) {
      if (only) {
        if (node.deep == depth) {
          resultList.add(node);
        } else {
          next.addAll(node._childrenList);
        }
      } else {
        resultList.add(node);
        next.addAll(node._childrenList);
      }
    }
    tmp = next;
    next = [];
  }
  return resultList;
}