首页 > 其他 > 详细

[leetcode] Maximum Depth of Binary Tree

时间:2016-03-08 10:43:43      阅读:214      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!