码迷,mamicode.com
首页 > 其他好文 > 详细

104. Maximum Depth of Binary Tree

时间:2018-04-24 11:07:18      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:com   ret   roo   深度   efi   alt   style   ima   root   

技术分享图片

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

 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

104. Maximum Depth of Binary Tree

标签:com   ret   roo   深度   efi   alt   style   ima   root   

原文地址:https://www.cnblogs.com/sssysukww/p/8926186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!