关闭

Maximum Depth of Binary Tree Java

标签: LeetCodeMaximum Depth of Bin
240人阅读 评论(0) 收藏 举报
分类:

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.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left,right)+1;
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32100次
    • 积分:871
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:6篇
    • 译文:0篇
    • 评论:5条
    Mylink
    最新评论