求给定二叉树的最大深度,
深度是指树的根节点到任一叶子节点路径上节点的数量。
最大深度是所有叶子节点的深度的最大值。
数据范围:,树上每个节点的val满足
要求: 时间复杂度
import java.util.*;
/*
- public class TreeNode {
- int val = 0;
- TreeNode left = null;
- TreeNode right = null;
- }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
if (root == null) {
return 0;
}
int nodeLeft = maxDepth(root.left);
int nodeRight = maxDepth(root.right);
return Math.max(nodeLeft, nodeRight) + 1;
}
}