求一棵树的最大深度 [英] Finding maximum depth of a tree
本文介绍了求一棵树的最大深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何找到二叉树的深度.但我不能将它概括为适用于任何树.
I know how to find the depth of a binary tree. But I cannot generalize it to work for any tree.
有人可以概述一个用于查找树(不一定是二叉树)深度的伪代码.
Can someone please outline a pseudo code for finding the depth of a tree (not necessarily a binary tree).
推荐答案
int findDepthOfTree(tree):
int deepest = 0;
for (child of root node)
deepest = max(deepest, findDepthOfTree(child))
return deepest + 1
这篇关于求一棵树的最大深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文