二叉树的最大深度
1.题目链接
2.思路
分治:
1.空树 高度为0
2.非空 分解子问题
先求左右子树的深度
总深度等于左右子树大的加1
3.实现
int maxDepth(struct TreeNode* root)
{
if (root == NULL)
return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
4.运行结果
#22二叉树的最大深度#完