🚩传送门:牛客题目
此题目其他解法:Ar 169. 多数元素
题目
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
例如:给定二叉树 [3,9,20,null,null,15,7] ,返回它的最大深度 3 。
解题思路:递归
我的代码
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
//返回左右子树深度更大的加上当前root的一层
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}