私の問題は、ツリー内にvalue
というノードの深度を返すように求めています。私はdepth(root, 7, 0 [depth initially])
をしなかった場合値のバイナリ検索ツリーを持つ特定のノードの深さを返します
例えば、それは2
私の最初の試みを返す必要があり、私はこの
# value is the value wanted, count measures the 'depth'
def depth(root, value, count):
# if we are not at a empty node
if root != None:
# if we found our data, then just return the count (depth)
if root.data == value:
return count
# otherwise increase count, and traverse both sides
else:
count += 1
count = depth(root.left, value, count)
count = depth(root.right, value, count)
return count
のようなもの、私は深さを得るのに、私はこれを実行しました= 6、なぜ私は分かりません
申し訳ありませんが私の代わりに間違いでした。私は正しい方向に切り替えるつもりでしたが、コードを間違ってコピーしたようです。私はそれを編集しました –