public int maxDepth(TreeNode root) {
return order(root, 0);
}
public int order(TreeNode node, int depth) {
if(node == null) {
return depth;
}
depth++;
int d1 = order(node.left, depth);
int d2 = order(node.right, depth);
return Math.max(d1, d2);
}深度优先搜索,把左右子树最大深度往上传。
本文介绍了一种计算二叉树的最大深度的方法,通过递归实现深度优先搜索,遍历左右子树并返回最大深度。
442

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



