网站设计名称,做礼品公司网站的费用,郑州pc网站建设,江苏省住房和城乡建设网站给定一个二叉树 root #xff0c;返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1#xff1a; 输入#xff1a;root [3,9,20,null,null,15,7]
输出#xff1a;3示例 2#xff1a;
输入#xff1a;root [1,null,2]
输出…给定一个二叉树 root 返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1 输入root [3,9,20,null,null,15,7]
输出3示例 2
输入root [1,null,2]
输出2提示树中节点的数量在 [0, 104] 区间内。
思路根节点深度当前根节点深度max左子树深度右子树深度注意每次计算子树深度时从0开始计数 public int maxDepth(TreeNode root) {return depth(root,0);}public int depth(TreeNode root,int height){if(root!null){height;heightMath.max(depth(root.left,0),depth(root.right,0));}return height;}