本文共 638 字,大约阅读时间需要 2 分钟。
# 递归解法class Solution: def TreeDepth(self, pRoot): if pRoot == None: return 0 else: return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right)) + 1
#非递归解法class Solution: def TreeDepth(self, pRoot): if not pRoot: return 0 a = [pRoot] d = 0 while a: b = [] for node in a: if node.left: b.append(node.left) if node.right: b.append(node.right) a = b d = d+1 return d
转载地址:http://ayfab.baihongyu.com/