leetcode -- 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.

Recursive Version

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int maxDepth(TreeNode root) {
12         // Start typing your Java solution below
13         // DO NOT write main() function
14         if(root == null){
15             return 0;
16         }
17         int left = 0, right = 0;
18         if(root.left != null){
19             left = maxDepth(root.left);
20         }
21         
22         if(root.right != null){
23             right = maxDepth(root.right);    
24         }
25         
26         return 1 + Math.max(left, right);
27     }
28 }
原文地址:https://www.cnblogs.com/feiling/p/3258632.html