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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its depth = 3.

思路就是DFS, 然后返回children 的depth的最大值 + 1, 依次循环, base case就是None, return 0.

1. Constraints

1) root : empty => 0

2. Ideas

DFS     T: O(n)      S; O(n)  # need to save all the temprary depth for each children

3. Code

 class Solution:
def maxDepth(self, root):
if not root: return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

4. Test cases

1) None => 0

2) 2 => 1

3)

    3
/ \
9 20
/ \
15 7

[LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS的更多相关文章

  1. [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS

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

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

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

  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 二叉树的最大深度

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

  6. 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 ...

  7. LeetCode 104. Maximum Depth of Binary Tree

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

  8. 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 long ...

  9. Java [Leetcode 104]Maximum Depth of Binary Tree

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

随机推荐

  1. Karma和Jasmine自动化单元测试

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

  2. win7 64位系统下读写access数据库以及安装了office32位软件再安装64位odbc的方法

    公司一款软件还在读写access数据库. 问题是我的电脑是win7 64位, 运行程序会报错, 出错信息很明显, 大意是ODBC数据源读写出错. 因此,我需要下载Access ODBC 64位数据源 ...

  3. Free Download Manager (FDM) 中文版 - 替代迅雷最佳免费开源下载工具软件

    https://www.freedownloadmanager.org/ Free Download Manager (FDM) 是一款经典免费纯粹的下载软件,它开源无广告,界面简洁清爽,支持 BT. ...

  4. mac下编译 boost编译工具b2

    cd boost_1_64_0/tools/build ./bootstrap.sh --with-toolset=gcc 输出: -n Bootstrapping the build engine ...

  5. 题目1008:最短路径问题(最短路径问题dijkstra算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1008 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. window上安装pymysql

    date: 2018-11-26   18:54:04 安装: cmd: pip install pymysql 验证: cmd: python >>import pymysql 不报错即 ...

  7. ConfluenceRemoteUserAuth

    配置confluence使用httpHeader的方式进行登录(目标版本:atlassian-confluence-6.3.3) 前提是已经安装好了Confluence,并且前端使用apache或者n ...

  8. python3安装builtwith

    >>> import builtwith Traceback (most recent call last): File , in <module> File excep ...

  9. Centos6.5安装mysql 5.7

    1.在官网下载安装包:https://dev.mysql.com/downloads/mysql/5.7.html#downloads mysql-5.7.10-linux-glibc2.5-x86_ ...

  10. Timer应用之Interval优化

    开发中, 有时有这种场景,使用 Timer 的 Timer_Elapsed 间隔  执行(如:从数据库)获取数据 与 现有 应用服务器中的 静态变量数据(起到缓存的目的)做 对比 ,若有改变,则 更新 ...