Leetcode:104. 二叉树的最大深度
要点:利用递归,只看一个头节点须要做哪些事。本题中,头节点只需看它的左子树和右子树哪个的深度最大,取其中最大的深度,再在最大深度根底上+1就是本节点的最大深度。
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return Math.max(l,r) + 1;
}
}
发表回复