题解 | #二叉树的深度#
二叉树的深度
http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
递归解法
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return int整型
#
class Solution:
def calDepth(self, pRoot: TreeNode, depth):
if not pRoot:
return depth
return max(self.calDepth(pRoot.right, depth+1), self.calDepth(pRoot.left, depth+1))
def TreeDepth(self , pRoot: TreeNode) -> int:
# write code here
return self.calDepth(pRoot, 0)
查看20道真题和解析
海康威视公司福利 1216人发布