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...
  • tzh476
  • tzh476
  • 2016年07月03日 19:06
  • 551

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
  • 233

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

计算二叉树的最大深度

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

Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)

Leetcode 104. Maximum Depth of Binary Tree (Easy) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月22日 16:50
  • 292
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:104. Maximum Depth of Binary Tree
举报原因:
原因补充:

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