import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
// 调用递归序遍历解决二叉树最大深度问题
return process(root);
}
// 递归序遍历二叉树函数 - 返回以root为根结点的子树的最大深度
public int process(TreeNode root) {
// 递归出口
if (root == null) {
// 当结点为空时,说明这一层的深度为0
return 0;
}
// 记录左子树和右子树的最大深度
int leftDepth = process(root.left);
int rightDepth = process(root.right);
// 返回左右子树最大深度的较大值,再加上本节点的1层深度
return Math.max(leftDepth, rightDepth) + 1;
}
}