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.


题目标签:Tree
  这道题目给了我们一个二叉树,要我们找到最大深度,就是从root点到最深的那个点之间点的数量。利用post order 来遍历二叉树,对于每一个点,它的两个children会返回两个depth值,取一个大的, 加上1继续返回。当遍历到最深的level,当一个点node == null的时候,说明走到底了,返回0。
 
 

Java Solution:

Runtime beats 15.89%

完成日期:07/01/2017

关键词:Tree

关键点:post order 来遍历树; Math.max 来取一个更大的值(从两个children返回的depth)

 /**
* 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)
{
int depth = 0; if(root == null)
return depth; depth = Math.max(maxDepth(root.left), maxDepth(root.right)); return depth + 1;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)的更多相关文章

  1. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

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

  2. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

  3. [LeetCode] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)

    描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

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

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  5. Leetcode 104 Maximum Depth of Binary Tree 二叉树

    计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...

  6. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  7. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

  8. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  9. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  10. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

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

随机推荐

  1. python基础之元组,集合

    一.元组 为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(,[,],,)) #t=tuple((,[,],,))) print(type(t)) 元组可以作为字典的key d={(,, ...

  2. Server Tomcat v7.0 Server at localhost failed to start.解决方法

    ---恢复内容开始--- 今天在做jsp项目的时候,Tomcat突然无法打开了,出现的报错是这样的: 也不知道是怎么搞得,百度了老半天看到有的网友是通过两这个方法解决的,连接在这http://blog ...

  3. Python装饰器主要用法

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' user = "admin" passwd = ...

  4. (一)关于java泛型的学习总结(泛型方法、泛型擦除)

    目录概要 一.泛型方法 二.利用泛型方法的特性实现代码的简化 三. 关于泛型的擦除 四.无界通配符和原生类型区别 五.转型和警告   泛型 一般的类中的属性或方法的参数,只能使用具体的类型:要么是基本 ...

  5. iOS开发者的管理工具-CocoaPods安装

    1. 安装 Ruby 对于iOS开发者,CocoaPods是最方便使用的第三方管理工具了,但是怎么安装CocoaPods呢,安装CocoaPods之前,要确保mac已经安装上Ruby,但在安装Ruby ...

  6. 程序员的自我修养九Windows下的动态链接

    9.1 DLL简介 DLL即动态链接库的缩写,它相对于Linux下的共享对象. Windows下的DLL文件和EXE文件实际上是一个概念,它们都是有PE格式的二进制文件. 微软希望通过DLL机制加强软 ...

  7. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  8. Ngnix技术研究系列1-通过应用场景看Nginx的反向代理

    随着我们业务规模的不断增长,整个系统规模由两年前的几十台服务器,井喷到现在2个数据中心,接近400台服务器,上百个WebApi站点,上百个域名. 这么多的WebApi站点这么多的域名,管理和维护成本很 ...

  9. 【BZOJ】2190 [SDOI2008]仪仗队(欧拉函数)

    Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...

  10. MySQL之常用函数

    MySQL有如下常用函数需要掌握: 1.数学类函数 函数名称 作用 ABS(x)   返回x的绝对值                      SQRT(x)   返回x的非负二次方根 MOD(x,Y ...