path = 0 # the lenght of the path
while self.right != None or self.left != None:
while self.right != None:
self = self.right
path = path +1
while self.left != None:
self = self.left
path = path +1
return path
这是我用来查找树高度的示例代码。树的高度被定义为从根节点到叶子节点的最长路径上节点数量,叶子节点的高度为1。
它不起作用。