原题链接在这里:https://leetcode.com/problems/maximum-depth-of-binary-tree/
求左右最大深度recursively, 比较出最大的再加一,终止条件是root == null, 返回0.
进阶题目是Balanced Binary Tree.
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right)+1;
}
}