1.題目: 二叉樹的最大深度。
給定一個二叉樹,找出其最大深度。 二叉樹的深度爲根節點到最遠葉子節點的最長路徑上的節點數。
2.解法
/**
* 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 Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}