二叉树的最大深度

tech2026-09-28  5

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

class Solution { public int maxDepth(TreeNode root) { if(root == null){ return 0; } if(root.left == null && root.right == null){ return 1; } //采用递归得方式,选取其中最大的路径值 int maxL = Math.max(maxDepth(root.left),maxDepth(root.right))+1; return maxL; } }
最新回复(0)