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 };

原文链接: https://www.cnblogs.com/mengqingzhong/archive/2013/05/11/3073018.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88047

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:24
下一篇 2023年2月9日 下午11:24

相关推荐