Maximum Depth of Binary Tree Java

原创 2015年07月08日 11:40:36

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;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode解题之Maximum/Minimum Depth of Binary Tree Java版(树的最大、最小深度)

leetcode解题之104# Maximum Depth of Binary Tree Java版(树的最大深度),111. Minimum Depth of Binary Tree(树的最大深度...

Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short...

No104. Maximum Depth of Binary Tree

一、题目描述 Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along t...

Leetcode-104. Maximum Depth of Binary Tree c语言

来源:104. Maximum Depth of Binary Tree 题目要求: Given a binary tree, find its maximum depth. The max...

LeetCode104 Maximum Depth of Binary Tree

详细见:leetcode.com/problems/maximum-depth-of-binary-tree Java Solution: github package leetcod...

Maximum Depth of Binary Tree

Problem:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along...

Maximum Depth of Binary Tree

问题描述:Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum de...

开荒新系列之LeetCode: 002-Maximum Depth of Binary Tree

总结讨论在不看上一篇文章的前提下,回忆总结一下上次工作,看看到目前为止还记得多少: 要多手写一些测试数据 不止一种方法实现,寻找最简单的方法 python is 比 == 要快一点点 Maximum ...

Maximum Depth of Binary Tree --- 求二叉树的最大深度

LeetCode练习题

104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)