我的json结构就像一个树结构。给定树结构的最大深度
-1
A
回答
0
我花了一段时间才了解counter
的数据结构我希望复制如何达成解决方案,但它只是松散地基于发布的代码(不适用于初学者的变量temp
)。
理解以下代码的一个关键是paramList
和svalue
对象具有相似的结构(如果不是相同的话)。
function findCounterDepth(counter)
{ var maxDepth = 0;
counter.algoList.forEach(function(algo)
{ findParamListDepth(algo.paramList, 0);
});
function findParamListDepth(paramList, depth)
{ ++depth;
paramList.forEach(function (paramObj)
{ var child = paramObj.svalue || paramObj.paramList;
if(child)
{ findParamListDepth(child, depth);
return;
}
});
maxDepth = Math.max(maxDepth, depth);
}
return maxDepth;
}
findCounterDepth(counter);
//= 4
编辑
添加叶节点的每算法对象的计数是不复杂的。这里计数添加为算法对象的dataLeafCount
属性:
function findCounterDepth(counter)
{ var maxDepth = 0;
var leafCount = 0; // add a leaf counter
counter.algoList.forEach(function(algo)
{ leafCount = 0; // zero leaf count per algorithm object
findParamListDepth(algo.paramList, 0);
algo.dataLeafCount = leafCount; // store as "dataLeafCount" property
});
function findParamListDepth(paramList, depth)
{ ++depth;
paramList.forEach(function (paramObj)
{ var child = paramObj.svalue || paramObj.paramList;
if(child)
{ findParamListDepth(child, depth);
return;
}
++leafCount; // it doesn't have children, increment leaf count;
});
maxDepth = Math.max(maxDepth, depth);
}
return maxDepth;
}
findCounterDepth(counter); //= 4
counter.algoList[0].dataLeafCount; //= 4
counter.algoList[1].dataLeafCount; //= 4
注意,如果输入对象不变性需要维护,findCounterDepth
应该进行修改,以在新的数据对象返回值。
相关问题
- 1. 树结构的最大深度
- 2. B型树的最大深度
- 3. 寻找树的最大深度
- 4. 查找二叉树的最大深度
- 5. 查找树的最大深度
- 6. 角UI树限制最大深度
- 7. 什么是C#中固定深度树状数据的最佳数据结构?
- 8. 给定深度处的子树中的节点数,给定主树中所有节点的深度
- 9. 大小为1的二叉树的最大深度
- 10. 确定树的深度
- 11. Sqlite的错误:表达式树太大(最大深度1000)
- 12. 给定结构的大小
- 13. 表达式树太大(最大深度1000)sqlite windows
- 14. UINavigationController中的UITableViewController来表示未知深度的树结构
- 15. 的MongoDB - 骨料深树结构
- 16. 找到最小化树深度的根
- 17. 计算树的深度Java数据结构
- 18. 如何获得html树的最大深度?
- 19. FETCH JOIN最大深度?
- 20. 瓶最大递归深度
- 21. Python:最大递归深度
- 22. CakePHP:轻松复制/深度复制树结构?
- 23. 树结构,动态深度和数据,仅留下
- 24. R:深度最小生成树
- 25. 给定深度的Swift二叉树列表节点
- 26. pylint给出了一些最大递归深度的错误
- 27. php/Mysql最佳树结构
- 28. 更新深度嵌套结构的Clojure
- 29. 找到树的深度?
- 30. SQL中的树深度
这必须是我读过的最通用的标题之一。 **每个**编程问题都是关于特定场景的编码逻辑。请尽量为读者提供更多帮助。 – Barmar
***但它没有给出正确的结果***并没有告诉我们你的问题是什么或你想要什么帮助。究竟是什么问题?你观察到什么?结果应该是什么?要在这里获得帮助,您需要非常具体地了解问题的具体内容。请记住,我们无法读懂你的想法。我们不知道你在找什么结果。我们只能阅读你的文字和代码。 – jfriend00