[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) 如果不加外面一层括号,错误

原文地址:https://www.cnblogs.com/yuchenkit/p/5252998.html