/**
* 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;
}
return maxDepthCore(root);
}
public int maxDepthCore(TreeNode root){
int count = 0;
int left = 0;
int right = 0;
if(root == null){
return 1;
}
if(root.left != null){
left = maxDepthCore(root.left);
}
if(root.right != null){
right = maxDepthCore(root.right);
}
return Math.max(right,left)+1;
}
}
没什么好说的,递归即可。可是时间复杂度有点低。
重新刷了一下,时间又低了。