题解 | #二叉树的最大深度#
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) { // write code here if (root == null) { return 0; } int leftmax = maxDepth(root.left); int rightmax = maxDepth(root.right); if (leftmax > rightmax) return leftmax + 1; else return rightmax + 1; } }