我想获得不确定树的最大深度。树看起来像下面,我怎样才能完成它

i wanna get the max depth of a not-certain tree. the tree looks like below,how can i finish it

给定一个树结构的数据,得到树的最大高度。我想获得不确定树的最大深度。树如下所示:

{
      id: 1,
      label: 'label1',
      children: [{
        id: 3,
        label: 'label2',
        children: [{
          id: 4,
          label: 'label3'
        }, {
          id: 5,
          label: 'label4',
          disabled: true,
          children: [{
              id: 4,
              label: 'label3'
            }, {
              id: 5,
              label: 'label4',
              disabled: true
            }]
        }]
      }

我试过如下,但没有达到预期效果。

const maxDepth = o => {

  if(!o || !o.children) return 0;

  let arr = []

  for(let i = 0; i< o.children.length; i++) {
    arr[i] = maxDepth(o.children[i])
  }

  let max = Math.max(...[arr]) + 1
  return max
}

我不相信你的数据格式是 100% 正确的,所以我冒昧地这样做了。话虽如此,这对递归算法来说是尖叫。

{
  id: 1,
  label: 'label1',
  children: [{
    id: 3,
    label: 'label2',
    children: [{
      id: 4,
      label: 'label3'
    },
    {
      id: 5,
      label: 'label4',
      disabled: true,
      children: [{
        id: 4,
        label: 'label3'
      },
      {
        id: 5,
        label: 'label4',
        disabled: true
      }]
    }]
  }]
}

test1 = {
  id: 1,
  label: "test1",
  children: []
}

test2 = {
  id: 2,
  label: "test1",
  children: [
  {
    id: 2,
    label: "test2",
    children: []
  },
  {
    id: 2,
    label: "test2",
    children: []
  }]
}

test3 = {
  id: 3,
  label: "test1",
  children: [
  {
    id: 3,
    label: "test2",
    children: [{
      children: [{
        children: [{}]
      }]
    }]
  },
  {
    id: 3,
    label: "test2",
    children: [{}]
  }]
}

your_data = {
  id: 1,
  label: 'label1',
  children: [{
    id: 3,
    label: 'label2',
    children: [{
      id: 4,
      label: 'label3'
    },
    {
      id: 5,
      label: 'label4',
      disabled: true,
      children: [{
        id: 4,
        label: 'label3'
      },
      {
        id: 5,
        label: 'label4',
        disabled: true
      }]
    }]
  }]
}

my_data = {
  id: 1,
  label: 'label1',
  children: [{
    id: 3,
    label: 'label2',
    children: [{
      id: 4,
      label: 'label3'
    },
    {
      id: 5,
      label: 'label4',
      disabled: true,
      children: [{
        id: 4,
        label: 'label3'
      },
      {
        id: 5,
        label: 'label4',
        disabled: true
      }]
    }]
  },
  {
    id: 6,
    label: 'madeup1',
    children: [{
      id: 7,
      label: 'madeup2',
      children: [{
        id: 8,
        label: 'madeup3',
        children: [{
          id: 9,
          label: 'madeup4'
        }]
      }]
    }]
  }]
}

function max_depth(exploringTheDepthsOf)
{
    largest = 0;
    if (exploringTheDepthsOf.hasOwnProperty('children'))
    {
        for (var i = 0; i < exploringTheDepthsOf["children"].length; i++)
        {
            largest = Math.max(largest, max_depth(exploringTheDepthsOf["children"][i]));
        }
    }
    else
    {
        return 0;
    }
    return largest + 1;
}

console.log("returned value", max_depth(test1));
console.log("returned value", max_depth(test2));
console.log("returned value", max_depth(test3));
console.log("returned value", max_depth(your_data));
console.log("returned value", max_depth(my_data));

这已经是我所能得到的最接近的了。 Geeks for Geeks 有一篇很好的文章和 javascript 代码来告诉你如何去做,但它是针对实际节点的,而不是针对 json 类对象:https://www.geeksforgeeks.org/depth-n-ary-tree/