104. Maximum Depth of Binary Tree

原创 2016年08月30日 19:29:28

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瞬间解决。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        if (root)
            depth=testMaxDepth(root->left,root->right);
        else
            return 0;
        return depth;
    }
    int testMaxDepth(TreeNode* left,TreeNode* right)
    {
        int depth=1;
        if (left&&right)
            depth+=max(testMaxDepth(left->left,left->right),testMaxDepth(right->left,right->right));
        else if (left) depth+=testMaxDepth(left->left,left->right);
        else if (right) depth+=testMaxDepth(right->left,right->right);
        return depth;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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...

LeetCode104 Maximum Depth of Binary Tree

详细见:leetcode.com/problems/maximum-depth-of-binary-tree Java Solution: github package leetcod...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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