/**
* 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;
}
if(root.left == null && root.right == null) {
return 1;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return l >= r? l+1:r+1;
}
}
leetcode-java-104. Maximum Depth of Binary Tree
最新推荐文章于 2022-03-21 00:00:00 发布
本文介绍了一种计算二叉树最大深度的算法实现。通过递归的方式,该算法能够遍历二叉树的每一个节点,并计算出从根节点到最远叶子节点的最长路径长度。
432

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



