描述
求给定二叉树的最大深度,
最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @return int整型
*/
function maxDepth( root ) {
// write code here
if(!root) return 0;
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1
// 深度遍历+递归 +1是root节点
}
module.exports = {
maxDepth : maxDepth
};
这篇博客介绍了如何通过深度优先搜索策略来计算给定二叉树的最大深度。利用递归的方法,计算左子树和右子树的最大深度,并取较大者加上根节点作为当前树的最大深度。这是一种有效的算法解决方案。
625

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



