104. Maximum Depth of Binary Tree

The problem is to find the maximum depth of the tree, the code is as follows:

 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324740696&siteId=291194637