leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def maxDepth(self, root):
if root == None:
return 0
l,r = 1,1
if root.left != None:
l = l+self.maxDepth(root.left)
if root.right != None:
r = r+self.maxDepth(root.right)
if l>r:
return l
else:
return r
1、在开始的时候经常遇到“NoneType”Error,后来查阅资料才知道使用 root==None 就可以处理这种情况;
2、调用函数的时候不需要传递self值,这个应该是Python自己传递的,如果自己添加上,反而会报错;
3、这道题目说明不是很清晰,系统的输入是{},{0},{0,0,0,0,#,#,0,#,#,#,0},{1,2}等形式,然后后台会自动构建二叉树;
leetcode:Maximum Depth of Binary Tree【Python版】的更多相关文章
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [LeetCode] 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 python
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- Leetcode 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] 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] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Minimum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
随机推荐
- 20170523xlVBA多条件分类求和一例
Public Sub NextSeven_CodeFrame() Application.ScreenUpdating = False Application.DisplayAlerts = Fals ...
- Android之封装好的异步网络请求框架
1.简介 Android中网络请求一般使用Apache HTTP Client或者采用HttpURLConnection,但是直接使用这两个类库需要写大量的代码才能完成网络post和get请求,而使 ...
- President's Path CodeForces - 416E (最短路,计数)
大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...
- h5 plus/h5+规范使用,模块索引,教你如何去看h5+的手册
最近看了下h5+规范的官网,开始觉得晦涩难懂,确实很乱,不过这也是基于我不理解的情况,终于艰难读完了,现在来分享下心得吧,基本看完文章,按我的方法,应该可以直接上手项目. 我准备的工具 hbuilde ...
- hdu-2897-巴什博弈
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Java远程调试 java -Xdebug各参数说明
JAVA自身支持调试功能,并提供了一个简单的调试工具--JDB,类似于功能强大的GDB,JDB也是一个字符界面的 调试环境,并支持设置断点,支持线程线级的调试 JAVA的调试方法如下: 1.首先支持J ...
- 一篇分析诊断被"hang"住数据库的资料(Oracle Performance Diagnostic Guide——Hang/Locking)
该资料已上传至本人QQ群空间,如需该资料,可到本人QQ群空间查找.下面贴表文本: Oracle Performance Diagnostic GuideHang/LockingVersion 3.1. ...
- 使用GAN进行异常检测——可以进行网络流量的自学习哇,哥哥,人家是半监督,无监督的话,还是要VAE,SAE。
实验了效果,下面的还是图像的异常检测居多. https://github.com/LeeDoYup/AnoGAN https://github.com/tkwoo/anogan-keras 看了下,本 ...
- PHP:第四章——数组中的排序函数
<pre> <?php header("Content-Type:text/html;charset=utf-8"); //1) /*sort - 对数组进行升序 ...
- hadoop redis install (4)
reference: http://dblab.xmu.edu.cn/blog/131/ https://github.com/dmajkic/redis https://blog.csdn ...