// you can also use includes, for example:
// #include <algorithm>
int getDepth(tree* T)
{
if(!T) return 0;
return max(getDepth(T->l), getDepth(T->r))+1;
}
int solution(tree * T) {
// write your code in C++98
return getDepth(T)-1;
}[codility]Tree-height
本文介绍了一个简单的算法,用于计算给定树结构的最大深度。通过递归地遍历树的左子树和右子树,该算法能够有效地确定整棵树的深度。


被折叠的 条评论
为什么被折叠?



