擅长:python、mysql、java
<p>如果我没弄错的话,你要解决的问题是:<strong>树中<code>value</code>值的最大深度是多少。</strong></p>
<p>您不仅应该在<code>t.value == value</code>时增加计数<strong>,而且还应该在树的任何子代与您要查找的值匹配时增加计数。这是因为你在测量深度。在</p>
<p>以下是算法的外观:</p>
<pre><code>def max_depth(t,value):
if t == None:
return -1
left = max_depth(t.left, value)
right = max_depth(t.right, value)
if t.value == value or left > -1 or right > -1: # <<<<
return 1 + max(left,right)
else:
return max(left,right) # This is always -1
</code></pre>