题解 | #二叉树的深度#
二叉树的深度
http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return int整型
#
class Solution:
def TreeDepth(self , pRoot: TreeNode) -> int:
quene = []
if pRoot:
quene.append(pRoot)
else:
return 0
count = 0
while quene:
count += 1
size = len(quene)
for i in range(size):
cur = quene.pop(0)
if cur.left:
quene.append(cur.left)
if cur.right:
quene.append(cur.right)
return count