zoukankan      html  css  js  c++  java
  • Maximum Depth of Binary Tree

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12      int max=-1;
    13      void meng(TreeNode*root,int dep)
    14      {
    15          if((root->left==NULL)&&(root->right==NULL))
    16          {
    17              if(dep>max)
    18                  max=dep;
    19              return;
    20          }
    21          if(root->left)
    22              meng(root->left,dep+1);
    23          if(root->right)
    24              meng(root->right,dep+1);
    25      }
    26     int maxDepth(TreeNode *root) {
    27         // Start typing your C/C++ solution below
    28         // DO NOT write int main() function
    29          max=-1;
    30          if(root==NULL)
    31              return 0;
    32          meng(root,1);
    33          return max;
    34     }
    35 };
  • 相关阅读:
    R
    P
    O
    M
    二分算法的一些思考
    I
    H
    G
    5-46 新浪微博热门话题 (30分)——unfinished HASH
    BZOJ 1179: [Apio2009]Atm
  • 原文地址:https://www.cnblogs.com/mengqingzhong/p/3073018.html
Copyright © 2011-2022 走看看