这是我见过的最水的题
题目链接链接地址
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
{
return 0;
}
else
{
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
}
}
}
本文介绍了一种计算二叉树最大深度的递归算法。通过递归地遍历二叉树的左子树和右子树,找到从根节点到最远叶子节点的最长路径长度。使用Java实现了一个名为Solution的类及其方法maxDepth。
1374

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



