You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
459 B

2 years ago
package class06;
// 测试链接https://leetcode.com/problems/maximum-depth-of-binary-tree
public class Code04_MaximumDepthOfBinaryTree {
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
}
// 以root为头的树最大高度是多少返回
public static int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}