[leetcode] Maximum Depth of Binary Tree

Posted 白天黑夜每日c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] Maximum Depth of Binary Tree相关的知识,希望对你有一定的参考价值。

Given a binary tree, find its maximum depth.

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=0;
        if(!root) return depth;
        int a=maxDepth(root->left);
        int b=maxDepth(root->right);
        depth=1+ ((a>b)?a:b);
        return depth;
    }
};

1. 最开始:depth=depth+((a>b)?a:b) 错误

2. depth=1 + ((maxDepth(root->left)>max(Depth(root->right))?maxDepth(root->left):maxDepth(root->right)),有一个test时间超限

3. depth=1+((a>b)?a:b) 如果不加外面一层括号,错误

以上是关于[leetcode] Maximum Depth of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]题解(python):104 Maximum Depth of Binary Tree

LeetCode 104. Maximum Depth of Binary Tree

leetcode:Maximum Depth of Binary Tree

LeetCode104. Maximum Depth of Binary Tree

[leetcode] Maximum Depth of Binary Tree

leetcode104-Maximum Depth of Binary Tree