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. css笔记 - 张鑫旭css课程笔记之 overflow 篇

    overflow基本属性值 visible(默认值):超出依然显示 hidden :超出隐藏 scroll :超出,滚动显示.子元素不超出也会有滚动条的那条轨道. auto:如果超出,滚动显示.如果不 ...

  2. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  3. laravel curl post json

    <?php namespace App\BO; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; us ...

  4. Elasticsearch学习之深入搜索四 --- cross-fields搜索

    1. cross-fields搜索 一个唯一标识,跨了多个field.比如一个人,标识,是姓名:一个建筑,它的标识是地址.姓名可以散落在多个field中,比如first_name和last_name中 ...

  5. SecureCRT无法使用root账户远程连接ubuntu

    ========1.问题============ SecureCRT无法使用root账户远程连接ubuntu 用其他账户连接,正常 用root账户连接,不能连接 =========2.原因====== ...

  6. 解决 java.lang.IllegalThreadStateException: Thread already started. 错误

    extends:http://blog.csdn.net/liuhanhan512/article/details/7575386 android开发中,对线程的操作比较悲催,start一个线程后,必 ...

  7. Unity3D 面试三 ABCDE

    说说AB两次面试: “金三银四” 三月份末又面试过两家:共和新路2989弄1号1001这家找了我半天,哇好漂亮的办公大楼!问了保安才知道,这个地址是小区地址.另一家也是创业公司面试我的自称是在腾讯做过 ...

  8. Unity3D笔记二十 多媒体与网络

    1 游戏音频 1.游戏音乐:如游戏背景音乐 2.游戏音效:如开枪或打怪物时“砰砰”的游戏音效 Unity 3D游戏引擎共支持4种音乐格式的文件,具体如下. aiff:适用于较短的音乐文件,可用作游戏音 ...

  9. 优秀的第二外语学习网站:Lang-8

    想要找native speaker帮你提高自己的写作能力么? 目前了解到的这方面最好的网站:http://lang-8.com 在这个网站上,你可以随便写一些句子或文章,然后就会有native spe ...

  10. mysql max(),min()的优化

    Select tables optimized away(选择表优化)