leetCode题解之求二叉树最大深度

时间:2023-03-09 21:50:41
leetCode题解之求二叉树最大深度

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 ;
else
return max( +maxDepth(root->left) , +maxDepth(root->right) ) ;
}