树的深度: 一棵树最深的层级
树的广度:一棵树拥有的叶子数量(只要有children就不算叶子节点)
https://blog.csdn.net/hh1197787867/article/details/97514817
// 获取树的深度
const getTreeDep=(tree)=>{
let dep=1;
tree.forEach(node=>{
if(node.children&&node.children.length){
dep=Math.max(dep,getTreeDep(node.children)+1)
}
})
return dep;
}
// 获取树的广度
const handleGetTreeExtent=(tree)=>{
let leafs=0;
tree.forEach(node=>{
if(node.children&&node.children.length){
leafs+=handleGetTreeExtent(node.children)
}else{
leafs+=1;
}
})
return leafs;
}
网友评论