1、题目描述

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node。

输入一个二叉树,求出其最大深度。

二叉树的某个节点的深度定义为根节点到该节点的路径长,根的深度为1,依次类推。最大深度就是离根最远的节点的深度。

 

2、问题分析

对于二叉树而言,使用递归是最容易的一种解法。

 

3、代码

int maxDepth(TreeNode* root)
     {
         if(root == NULL)
             return 0;
         else
             return max( 1+maxDepth(root->left) , 1+maxDepth(root->right) ) ;
     }

 

相关文章:

  • 2021-07-21
  • 2021-08-31
  • 2021-09-23
  • 2021-08-22
  • 2021-11-27
  • 2021-07-29
  • 2022-01-25
  • 2021-07-14
猜你喜欢
  • 2022-12-23
  • 2021-04-23
  • 2021-05-11
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2021-08-31
相关资源
相似解决方案