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 = ...
随机推荐
- EF学习-获取实体框架01
实体框架由 EF 设计器(包含在 Visual Studio 中)和 EF Runtime(在 NuGet 上提供)组成. EF 设计器包含在 Visual Studio 中 最新版本的实体框架设计器 ...
- Java基础-this关键字和构造方法(10)
this关键字 方法被哪个对象调用,this就代表那个对象当局部变量隐藏成员变量时,使用this关键字(例如构造方法和访问器). 构造方法 构造方法作用概述 给对象的数据进行初始化 构造方法格式 方法 ...
- Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)
大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...
- python-day21--time模块
一.三种表示方法 1.时间戳(timestamp): time.time( ) #得到的是float类型 2.格式化(Format String): time.strftime('%Y/% ...
- BST(二叉排序树)的插入与删除
值得一说的是删除操作,删除操作我们分为三种情况: 1.要删的节点有两个孩子: 找到左子树中的最大值或者右子树中的最小值所对应的节点,记为node,并把node的值赋给要删除的节点del,然后删除nod ...
- 正向代理到指定泛域名的nginx配置
resolver 8.8.8.8; #必须配置!!!不然无法代理 server { listen default_server; listen [::]: default_server; server ...
- Apollo配置中心介绍
参考链接:https://github.com/ctripcorp/apollo/wiki/Apollo%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E4%BB%8B%E7 ...
- hadoop kafka install multi-borker (7)
multi-borker function like cluster technology First we make a config file for each of the brokers (o ...
- 日期控件:My97DatePicker
My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 下载地址:http://download.csdn.net/detail/emov ...
- SWIFT模糊效果
首先创建一个模糊效果 let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 接着创建一个承载模糊效果的视图let blurView ...