104. Maximum Depth of Binary Tree
Posted sysu_kww
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于104. Maximum Depth of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章
104. Maximum Depth of Binary Tree
LC.104. Maximum Depth of Binary Tree
104. Maximum Depth of Binary Tree
LeetCode 104. Maximum Depth of Binary Tree