#Get length of the longest path through recursion
def max_height(node):
if not node:
return 0
left = max_height(node.left) #Base Case based on my understanding
right = max_height(node.right) #Base Case based on my understanding
return max_height(left, right) + 1
我一直在调用max_height来获得长度,但我得到一个错误。我想到了三种可能性:递归和二叉树
1)我误解了基本案例的概念,实际上我没有基本案例。
2)我没有正确地分隔Python代码。
3)我没有递归地获得BST的高度,而是树的宽度,这影响了以后的计算。
我知道它与这个问题类似,但主要的区别是我真的试图使用递归,其中另一个问题使用迭代,只是将其称为递归。 how to find the height of a node in binary tree recursively
你得到了什么错误? – cyroxis