class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Tree:
def TreeDepth(self, pRoot):
if pRoot is None:
return 0
llength = self.TreeDepth(pRoot.left)
rlength = self.TreeDepth(pRoot.right)
return max(llength, rlength) + 1
root = TreeNode(10)
secondNode = TreeNode(9)
threeNode = TreeNode(8)
secondNode.left = threeNode
root.left = secondNode
s = Tree()
value = s.TreeDepth(root)
print('二叉树的深度是%d' % value)
力扣经典题解:
打印:
二叉树的深度是3