zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Permutations II
    N-Queens II
    Palindrome Number
    Minimum Path Sum
    JS的DOM操作2
    JS 的DOM操作
    函数概念
    JavaScript数组
    JavaScript循环及练习
    JS语言
  • 原文地址:https://www.cnblogs.com/sssysukww/p/8926186.html
Copyright © 2011-2022 走看看