LeetCode 104

Maximum Depth of Binary Tree

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.

 

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10  
11 public class Solution {
12     public int maxDepth(TreeNode root) {
13         return (root == null)?0:Math.max(1+maxDepth(root.left),1+maxDepth(root.right));
14     }
15 }

 

posted @ 2016-04-24 23:03  Juntaran  阅读(84)  评论(0编辑  收藏  举报