分享

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

 雪柳花明 2016-09-25

  

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多