104. Maximum Depth of Binary Tree

该题是要找出树的最大深度,代码如下:

 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 class Solution {
11     private int max = 0;
12     public int maxDepth(TreeNode root) {
13         
14         helper(root, 0);
15         
16         return max;
17     }
18     
19     private void helper(TreeNode root, int tmp){
20         
21         if(root == null){
22             if(tmp > max){
23                 max = tmp;
24             }
25             return ;
26         }
27         
28         tmp ++;
29         helper(root.left, tmp);
30         helper(root.right, tmp);
31     }
32 }

 

 

 

END

posted @ 2018-04-24 09:13  sysu_kww  阅读(80)  评论(0编辑  收藏  举报