104. Maximum Depth of Binary Tree

原创 2016年08月30日 10:29:51

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.

求二叉树的最大深度。

最大深度是根节点到最远叶子节点路径上经过的节点总数。


用前序遍历(DLR)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int maxDepth(struct TreeNode* root) {
    unsigned int depth=0;
    return pretrav(root,depth);
}
int pretrav(struct TreeNode* p, unsigned int d)
{
    int l,r;
    if(p!=NULL)                     //递归终止条件
    {
        d++;
        l=pretrav(p->left,d);       //遍历左子树
        r=pretrav(p->right,d);      //遍历右子树
        d=l>=r?l:r;                 //深度为左右子树中的较大值
    }
    return d;
}
提交结果如下:



版权声明:本文为博主原创文章,转载请声明出处:)

相关文章推荐

No104. Maximum Depth of Binary Tree

一、题目描述 Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along t...

Leetcode-104. Maximum Depth of Binary Tree c语言

来源:104. Maximum Depth of Binary Tree 题目要求: Given a binary tree, find its maximum depth. The max...
  • tzh476
  • tzh476
  • 2016年07月03日 19:06
  • 516

LeetCode104 Maximum Depth of Binary Tree

详细见:leetcode.com/problems/maximum-depth-of-binary-tree Java Solution: github package leetcod...
  • zxwtry
  • zxwtry
  • 2017年04月27日 22:00
  • 210

104/111 Maximum/Minimum Depth of Binary Tree(二叉树求深度)

104 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the...

leetCode 104. Maximum Depth of Binary Tree

题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/     题目内容: Given a binary tree, ...

LeetCode笔记:104.Maximum Depth of Binary Tree

计算二叉树的最大深度

104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth...

[LeetCode]104.Maximum Depth of Binary Tree

【题目】 Maximum Depth of Binary Tree  Total Accepted: 5260 Total Submissions: 11532My Submissions ...

LeetCode#104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum dep...

【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)

原题Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the lon...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:104. Maximum Depth of Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)