张家界网站建设多少钱,wordpress首页调用短代码,熊猫关键词工具,wcf网站开发题目
给定一个二叉树 root #xff0c;返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1#xff1a; 输入#xff1a;root [3,9,20,null,null,15,7] 输出#xff1a;3
解
class Solution {public int maxDepth(TreeNod…题目
给定一个二叉树 root 返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
示例 1 输入root [3,9,20,null,null,15,7] 输出3
解
class Solution {public int maxDepth(TreeNode root) {if (root null) {return 0;}if (root.left null root.right null) {return 1;} else if (root.left null) {return maxDepth(root.right) 1;} else if (root.right null) {return maxDepth(root.left) 1;}return Math.max(maxDepth(root.left), maxDepth(root.right)) 1;}
}