码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] Maximum Depth of Binary Tree

时间:2016-03-08 10:46:14      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!