关闭

Leetcode 104. Maximum Depth of Binary Tree

82人阅读 评论(0) 收藏 举报

<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">Given a binary tree, find its maximum depth.</span>

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;
        }
        return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
    }
}
需要再看,递归,非自己写出来的。



0
0
查看评论

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution Given a binary tree, find its maxi...
  • wangyaninglm
  • wangyaninglm
  • 2015-05-13 22:16
  • 3798

【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

【104-Maximum Depth of Binary Tree(二叉树的最大深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tree, find its maximum depth.   The maximum depth is...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-08 06:43
  • 2959

LeetCode104——Maximum Depth of Binary Tree

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 farthes...
  • booirror
  • booirror
  • 2015-02-05 15:29
  • 1529

【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree

今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的,因为c++版的代码网上比较多,所以就只分享一下Python的代码吧,刚学完Python的基本语法,做做LeetCode的题目还是不错的,对以后找工作面试也有帮助! 刚开始就从AC率最高的入手吧! ...
  • u011613729
  • u011613729
  • 2014-07-20 13:18
  • 3638

Maximum Depth of Binary Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会以一条链表为基准,比如说l1, 那么如果l1当期那的元素比较小,那么直接移动l1...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 03:34
  • 12713

104. Maximum Depth of Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/maximum-depth-of-binary-tree/题目原文 Given a binary tree, find its maximum depth. The maximum depth is the numbe...
  • coder_orz
  • coder_orz
  • 2016-05-07 13:21
  • 1411

【LeetCode】104. Maximum Depth of Binary Tree(DFS|BFS)

原题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 ...
  • jiange_zh
  • jiange_zh
  • 2016-01-07 10:05
  • 797

LeetCode 104 -Maximum Depth of Binary Tree ( JAVA )

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 farthes...
  • UckyK
  • UckyK
  • 2016-04-05 18:27
  • 604

Minimum Depth of Binary Tree -- LeetCode

原题链接:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/  这道题是树的题目,其实跟Maximum Depth of Binary Tree非常类似,只是这道题因为是判断最小深度,所以必须增加一个叶子的判断(因为...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 03:52
  • 14274

Leetcode 654. Maximum Binary Tree 最大二叉树 解题报告

就是给了一堆不重复的数组,然后挑选一个最大的作为root,然后root左边的和右边的有分别按照这个方法进行构建。 所以我做的方法也很简单咯,那么就每次找到这一段里最大的做root,然后递归左右两边。。简单粗暴的解法。。至于再高深的求各位大佬指教,我就不懂了哦,对了,说起来倒是有个优化策略,可以设置...
  • MebiuW
  • MebiuW
  • 2017-08-14 22:10
  • 1132
    个人资料
    • 访问:816次
    • 积分:104
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档