一、题目
给定root,返回最大深度,最大深度指,从根节点到最远子节点的最长路径上的节点数。

二、思路
1、层序遍历。层数就是最大深度。
2、深度优先搜索,深度是左节点深度或右节点深度的最大值+1。
三、代码
class Solution {
int res;
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
res = 1+ Math.max(leftDepth,rightDepth);
return res;
}
}
735

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



