题解#DFS求解最大深度#

二叉树的最大深度

http://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    int DFS(TreeNode* node){
        if(node==NULL)
            return 0;
        int depth=1;
        int left=DFS(node->left);
        int right=DFS(node->right);
        return left>right?depth+left:depth+right;
    }
    int maxDepth(TreeNode* root) {
        // write code here
        int depth=DFS(root);
        return depth;
    }
};
全部评论

相关推荐

牛客84809583...:举报了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务