Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
遞歸就好了,注意 min不可以這么解,因為這個方法返回null 為0;題目要求的是到葉子結點的。
/**
* 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) {
return find(root);
}
private int find (TreeNode root)
{
if(root==null)return 0;
return Math.max(find(root.right),find(root.left))+1;
}
}