继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

leetcode 104. Maximum Depth of Binary Tree

Freya0_0
关注TA
已关注
手记 34
粉丝 71
获赞 639

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

public class Solution {
    public int maxDepth(TreeNode root) { 
        int depth = 0;
        if(root != null){
            int leftDepth = maxDepth(root.left);
            int rightDepth = maxDepth(root.right);
            depth ++;
            if(leftDepth < rightDepth){
                depth = depth + rightDepth;
            }else{
               depth = depth + leftDepth;
            }
        }

        return depth;

    } 

}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP