104. 二叉树的最大深度
1.题目描述及示例
- 题目描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。 - 示例
2.题解思路及代码
- 思路
简单,略 - 代码
int max=0;
public int maxDepth(TreeNode root) {
if (root!=null)
{
preOrder(root,1);
return max;
}
return 0;
}
public void preOrder(TreeNode root,int depth)
{
if (root!=null)
{
max=Math.max(max,depth);
preOrder(root.left,depth+1);
preOrder(root.right,depth+1);
}
}