打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
104 [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

  

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.

 

求二叉树的最大深度问题用到深度优先搜索DFS,递归的完美应用,跟求二叉树的最小深度问题原理相同。代码如下:

 

class Solution {public:    int maxDepth(TreeNode* root) {        if (!root) return 0;        return 1 + max(maxDepth(root->left), maxDepth(root->right));    }};

 

求二叉树的最小深度可以参见我的博文:

http://www.cnblogs.com/grandyang/p/4042168.html

 

LeetCode All in One 题目讲解汇总(持续更新中...)

分类: LeetCode
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
​LeetCode刷题实战543:二叉树的直径
二叉树和分治法
LeetCode实战:二叉树的最大深度
一文弄懂二叉树三种遍历
0111. Minimum Depth of Binary Tree (E)
LeetCode算法题-Average of Levels in Binary Tree(Java实现)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服