如果树只有一个节点,那么它的深度为1;如果根节点有左子树也有右子树,那么树的深度就是其左右子树深度的较大值再加1
class Solution:
def TreeDepth(self, pRoot):
# write code here
if pRoot is None:
return 0
count = max(self.TreeDepth(pRoot.left), self.TreeDepth(pRoot.right)) + 1
return count
京公网安备 11010502036488号