题目描述:
思路:没什么特别的,直接递归。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null)return 0;
int left_len=maxDepth(root.left);
int right_len=maxDepth(root.right);
int result=Math.max(left_len,right_len)+1;
return result;
}
}
此外题解还给出了广度优先的做法,每层遍历最终记录经历了多少层