[LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
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的更多相关文章
- [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 ...
- [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 ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- redhat vi 命令
转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...
- Internet Message Access Protocol --- IMAP协议
https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol Internet Message Access Protocol
- remote: fatal: could not read Username for 'http://spapa.wicp.net:3000': No such device ors
解决办法: git remote add origin https://{username}:{password}@github.com/{username}/project.git in my ca ...
- sonarqube插件开发(二) 开发插件
一.环境准备 java 1.8, maven 3.1 检查自己的环境是否支持 sonarqube的插件开发 java -version mvn -version 二.创建maven项目 pom.xml ...
- 其它终端设备连接gmail账户提示密码错误解决方法
换新手机配置Google Account继续使用Gmail服务,输入用户名.密码进入状态同步一段时间后再次提示输入用户名.密码并显示账号信息不正确.网上有人提到"修改用户密码"再进 ...
- dig命令安装
yum -y install bind-utils Dig是一个在类Unix命令行模式下查询DNS包括NS记录,A记录,MX记录等相关信息的工具 查找yahoo.com的A记录:(此处一定是域而不是 ...
- [工具]Sublime 显示韩文
- 什么是webpack?
https://www.webpackjs.com/concepts/ https://webpack.github.io/ 本质上,webpack 是一个现代 JavaScript 应用程序的静态模 ...
- C++ 标准输出cout与printf
C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...
- 如何使QLineEdit禁止编辑
在写程序的时候喜欢使用QLineEdit,用来显示打开文件的路径.但是很不喜欢被编辑.那么要怎么设置不可编辑呢. (1)调用lineEdit->setEnabled(False) #不可编辑了 ...