题目描述
输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def TreeDepth(self, pRoot):
# write code here
if pRoot == None: //注意不要写成none,
return 0
ld=self.TreeDepth(pRoot.left)
rd=self.TreeDepth(pRoot.right)
return max(ld,rd)+1
补充知识点 Python 中None和Null区别 :
Python中的None与 NULL(即空字符)的区别
了解以上概念,就不难理解None 与null的区别
(1)是不同的一种数据类型
>>>type(None)
<class 'NoneType'>
1
2
表示该值是一个空对象,空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。
>>>type('')
<class ''str'>
1
2
你可以将None赋值给任何变量,也可以将任何变量赋值给一个None值得对象
(2)判断的时候 均是False
>>> ff=None
>>> if ff:
print('ff is define')
执行结果:无打印!
1
2
3
4
5
(3)属性不同
使用dir()函数返回参数的属性、方法列表。如果参数包含方法dir(),该方法将被调用。如果参数不包含dir(),该方法将最大限度地收集参数信息。
dir(None)
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
1
2
dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '