沒什么好說的,遍歷一層加1
public int maxDepth(TreeNode root) {
? ? ? ? if (root == null) return 0;
? ? ? ? return 1 + max(maxDepth(root.left), maxDepth(root.right));
? ? }
? ? private int max(int p, int q) {
? ? ? ? return p > q ? p : q;
? ? }
沒什么好說的,遍歷一層加1
public int maxDepth(TreeNode root) {
? ? ? ? if (root == null) return 0;
? ? ? ? return 1 + max(maxDepth(root.left), maxDepth(root.right));
? ? }
? ? private int max(int p, int q) {
? ? ? ? return p > q ? p : q;
? ? }