编辑代码

const measure2 = (node) => {
  // TODO write your code here
  if (!node) {
    return;
  }
  const q = [];
  let qIndex = 0;
  const l = [];
  q.push(node);
  q.push({ type: 'add' });
  while (q && q.length > qIndex) {
    const cur = q[qIndex++];
    l.push(cur);
    if (cur.children && cur.children.length > 0) {
      cur.children.forEach((c) => {
        q.push(c);
      });
      q.push({ type: 'add' });
    }
  }
  let le = 0;
  l.forEach((n) => {
    if (n.type === 'add') {
      le++;
    } else {
      console.log('id:', n.id, 'level', le);
    }
  });
};